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.

Submitted: 0 / 10

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.