Turn words into conditions
A comparison answers a yes-or-no question. Write age == 12 to test equality and age = 12 to assign a value. The two uses of the equals sign look similar, so read the whole line out loud. Other comparisons include != for “is different from”, < for “is below” and <= for “is at or below”. Phrases like “from 12 to 16 inclusive” mean both ends are accepted. Write age >= 12 and age <= 16 or the equivalent chained comparison 12 <= age <= 16.
Use and for requirements that must both hold. Use or where either condition is enough. To reject values outside 12 to 16, use age < 12 or age > 16. Check a number from the middle as well as each boundary. If you accidentally write age >= 12 or age <= 16, every integer satisfies at least one half. The code runs, but the decision rule is wrong.
| Age | age >= 12 | age <= 16 | Both true? |
|---|---|---|---|
| 11 | False | True | False |
| 12 | True | True | True |
| 15 | True | True | True |
| 16 | True | True | True |
| 17 | True | False | False |
Order your checks
Read the input as text first. Check presence and format before using int(). Compare the numeric value with the allowed range only after conversion succeeds. For the locker task, .isdecimal() is a format check. It returns false for the empty string, a minus sign or a decimal point. It allows leading zeros, which the brief permits. A range check is a different decision. "31" can be converted successfully even when it is outside the allowed range.
entry = input()
if not entry.isdecimal():
print("Check the format")
elif int(entry) <= 20:
print("In range")
else:
print("Too large")
Trace " 12", "020" and "21". The first starts with a space, so the format branch prints Check the format and Python skips the conversion. "020" passes the format check and converts to 20, which meets the inclusive limit. "21" passes format but exceeds the limit, so the final branch runs. The code allows 0 because the brief for this example supplies only an upper limit and the accepted format has no minus sign.
Keep separate decisions separate
An if / elif chain selects at most one branch. Two separate if statements can both run. Suppose a club offers a free badge for members and a free poster to anyone aged under 16. The same person might get both, so separate if statements can be appropriate. For a single ticket category, use one chain. Work out whether the brief requires one output or several before choosing the structure.
Practise a Paper 2 habit
A starter program should display Junior for ages under 12, Youth for 12 to 16, and Adult for 17 and above. Its first condition is age <= 16, followed by elif age < 12. Explain why a ten-year-old never gets Junior. Rewrite the conditions in the correct order. Choose three boundary inputs and predict their outputs. Then test one more age well away from a boundary to check the general case.
Validate while keeping the original evidence
Suppose a booking code must begin with B and have exactly four characters. Check len(code) == 4 first. A blank input cannot be indexed at position 0, but a false length condition short-circuits an and chain. Only after length passes do you inspect code[0] and the remaining characters. A format check answers whether the entry follows the rules, while a lookup answers whether it exists in a known list. A code can pass the format check and still be absent. Keep this distinction when interpreting messages such as Invalid format and Unknown code. To test the two paths separately, try one malformed code and one correctly formed code that you know is absent.