ENGG1003 Lab08 PythonBasics
ENGG1003 Lab08 PythonBasics
Bonus exercise
To try and practice while loop
Variables and Expressions
temperature = 24.6
humidity = 0.75 Key-in OR copy-and-paste
print(temperature > 24) one line at a time!
print(humidity > 0.85)
Practice on print(temperature >= 24 or humidity < 0.7)
print(22 < temperature < 28)
IDLE (Python) approx_dew_point = temperature - (100 - humidity * 100) / 5
print('Dew point =', approx_dew_point)
Execute a batch of codes
Feel troublesome?
If you have a batch of codes to be executed (like the codes
in the previous slide, it is good to create a python
(program) file by Python IDLE.
Start up the Python IDLE, you will see the Python Shell
environment.
This is Python Shell
environment. We can
enter and execute code
one-by-one only.
Execute a batch of codes
Click on “File” from menu ribbon, then click “New
File”, a new window with the name “untitled” will be
prompted.
This is the Python editor. We can type in a batch of
codes here and execute them together. It will help to
organize the execution of codes.
Check the code in the file. Read the comments. And begin
to write your code.
Task 1 - User Input: basic
mathematical operations
Anything that starts with # is a comment which is not executed
#Task 1: Calculate the circumference of the circle.
pi = 3.1416
# Display the input prompt "Enter the value of radius in cm :" to obtain r from user
Use the input( ) command along with a message “Enter the
r = int( ))
value of radius : ”, to request the value from the user Try test cases.
# Implement the equation to calculate the circumference of the circle
Given r : 3
# Fill your code on the line below…
Implement the formula Expected ans:
18.8496
# Print the message, "The circumference of the circle is :", followed by c
For example: When you run your code, the code must ask
the user to input a seat number that the user want to
reserve. Based on the seat number, your code should
charge the ticket to the user.
To start the task, download “task2.py” file from the
blackboard.
To open “task2.py” file, launch IDLE Python, click File
Open browse and pick task2.py and click Open.
Check the code in the file. Read the comments. And begin
to write your code.
Work on Task 2a First
# Task 2a : Implement the if-else statements
sn = int(input('Please enter the seat number you want to reserve for yourself
(enter a number from 1 to 100) : '))
if seat number (sn) is greater or equal to 1
# Implement the if-elif-else statements below:
and sn is less than or equal to 10 then:
ticket_price shall be 150
elif ...:
ticket_price shall be ...
elif ...:
ticket_price ...
else:
print("Invalid data")
# Task 2b: Check the status of student society membership information using if
ticket_price = 0
statement and implement the equation to calculate the new discounted ticket
exit()
price of the concert
print("You need to pay: $", ticket_price) Run Run Module [F5] to check your result.
Save the “task2.py” python file in your working folder.
Task 2b: Using user input function
and if-elif-else statements
sn = int(input('Please enter the seat number you want to reserve for yourself (enter a
number from 1 to 100) : '))
if ...:
ticket_price shall be 150
elif ...:
...
else:
...
# Task 2b: Check the status of student society membership information using if statement and
implement the equation to calculate the new discounted ticket price of the concert
# We shall remove the "#" from the line below, like this: Leading # sign removed
sm = input('Are you a member of student society? (enter y/n) : ')
# Implement the if statement here and equation to calculate the discounted price here
# Task 3: Print only the even numbers from 1 to 50 using a for loop and the modulo operator
Check the code in the file. Read the comments. And begin
to write your code.
#Task 4 - Bonus: Complete the guessing game (Complete the codes in red)
print('This game requires two people. One person will enter the password that is to be
guessed while the second person will guess the password.')
for i in range(50):
print("Scrolling screen to hide player 1's password...")
prints the length of the password
# Print some hints for player 2
print('Hint 1: The length of the password is :', len(password) ) prints the first letter of the password
# Complete the hints for the player 2 How to print the middle and the last
letter of the password?
print('Hint 3: The middle letter of the password is :', )