Chapter 0 · Section 3 practice
Comparisons and Decisions
Work from question 1 to 10. Write your code, click Run Code, review the result, then click Submit attempt. An attempt may be wrong: reference answers become available after you submit all 10. Each Python box is independent. Submission progress is saved in this browser.
1. At least 18?
Warm-up. Print whether age is at least 18. Use a comparison, not a quoted message.
Write your answer, then run it.
2. Assignment and equality
Warm-up. Assign 60 to score. Print whether score equals 60, then whether it is greater than 60.
Write your answer, then run it.
3. Strict or inclusive?
Warm-up. Print whether money exceeds cost, then whether money is enough to pay cost exactly.
Write your answer, then run it.
4. Both are required
Build your skills. Entry requires a ticket AND a student card. Print whether entry is allowed.
Write your answer, then run it.
5. At least one is enough
Build your skills. A visitor can pay with cash OR a bank card. Print whether they have a way to pay, then whether they lack cash.
Write your answer, then run it.
6. Choose a message
Build your skills. If score is at least 60, print Pass; otherwise print Try again. Also test exactly 60 and 59 yourself.
Write your answer, then run it.
7. Three categories
Build your skills. Print Hot for temperature >= 30, Warm for >= 20, and Cool otherwise. Use if / elif / else.
Write your answer, then run it.
8. Combine tests in a decision
Challenge. Allow a purchase only if money covers cost AND the item is in stock. Print Buy or Wait.
Write your answer, then run it.
9. Repair overlapping branches
Challenge. This order gives the wrong grade at score 95. Fix it: A >= 90, B >= 80, C >= 70, F otherwise. Test 90, 80, 70, and 69 too.
Write your answer, then run it.
10. Choose a ticket price
Challenge. Use the following classroom rules: under 12 pays HK$20; otherwise a student pays HK$30; everyone else pays HK$50. Set ticket_price and print it with two decimal places. Test an 11-year-old nonstudent and a 20-year-old nonstudent too.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. At least 18?
age = 17
print(age >= 18)Expected output
FalseAt least includes equality, so use >=.
2. Assignment and equality
score = 60
print(score == 60)
print(score > 60)Expected output
True
FalseAssignment uses =; comparison for equality uses ==.
3. Strict or inclusive?
money = 25
cost = 25
print(money > cost)
print(money >= cost)Expected output
False
TrueHaving exactly the cost is enough to pay, but it does not exceed the cost.
4. Both are required
has_ticket = True
has_student_card = False
print(has_ticket and has_student_card)Expected output
Falseand is true only when both Boolean inputs are true.
5. At least one is enough
has_cash = False
has_bank_card = True
print(has_cash or has_bank_card)
print(not has_cash)Expected output
True
Trueor needs at least one true condition; not reverses a Boolean.
6. Choose a message
score = 65
if score >= 60:
print("Pass")
else:
print("Try again")Expected output
PassThe indented line in the matching branch runs.
7. Three categories
temperature = 24
if temperature >= 30:
print("Hot")
elif temperature >= 20:
print("Warm")
else:
print("Cool")Expected output
WarmAt 24 the first test fails and the second is true.
8. Combine tests in a decision
money = 50
cost = 40
in_stock = False
if money >= cost and in_stock:
print("Buy")
else:
print("Wait")Expected output
WaitEnough money alone does not satisfy the stock requirement.
9. Repair overlapping branches
score = 95
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(grade)Expected output
APut the highest overlapping threshold first. These are teaching grades, not course policy.
10. Choose a ticket price
age = 16
is_student = True
if age < 12:
ticket_price = 20
elif is_student:
ticket_price = 30
else:
ticket_price = 50
print(f"HK${ticket_price:.2f}")Expected output
HK$30.00The under-12 rule has priority; only older visitors reach the student test.