Homework 2
Practical programming skills in Python
Homework 2
1. Comparison operators
Complete the table below:
Operator Name
==
!=
Less than
Greater than
<=
Greater than or equal to
[3]
2. Selection statements
Complete the following selection statements by filling in the blanks:
____ score ____ 1337:
print(“1337 is the maximum score – have 5 extra lives!”)
lives = lives + 5
____ score ____ 1000:
print(“Scoring 1000 or more grants you 3 extra lives!”)
lives = lives + 3
____ score ____ 500:
print(“That score is terrible. Have an extra life and try again.”)
lives = lives + 1
____:
print(“A pitiful attempt! Lose a life!”)
lives = lives - 1
[5]
1
Homework 2
Practical programming skills in Python
3. Choosing Loops
Explain how you would choose whether to use a FOR loop or a WHILE loop. [4]
FOR: ______________________________________________________________
WHILE: ______________________________________________________________
4. For Loops
Describe the outputs of the following programs
Program Output
for count in range(3):
print(“CS FTW!”)
for count in range(2):
print(count)
print(“Finished!”)
for count in range(3):
print(“This is step”, count+1)
for count in range(3):
print((count+1)*3)
[4]
2
Homework 2
Practical programming skills in Python
5. While Loops
Complete the table to explain the purpose of each of these while loops.
Program Explanation
lives = 5
while lives > 0:
# Play game, set loseALife to True or False
if loseALife == True:
lives = lives – 1
print(“Game Over!”)
realPpassword = “P@ssw0rd”
password = input(“Enter your password: ”)
while password != realPassword:
print(“Password incorrect”)
password = input(“Enter your password: ”)
again = “y”
while again == “y”:
# Perform some action
again = input(“Go again? y/n: ”)
bill = 0 Loop repeats until the user
budget = 100 goes over the budget amount
while _____________________:
price = float(input(“Price of item: ”))
bill = bill + price
print(“Sorry, just over budget!”)
[4]
[Total 20 marks]