Original practice · KS3 and GCSE
Three Python bugs that look similar but behave differently
A syntax error means Python cannot parse the instructions, a runtime error occurs while running, and a logic error produces the wrong result. Debugging is a process: predict what should happen, reproduce the issue, change one thing and test again.
1. Syntax error: missing colon
age = 14
if age >= 13
print("Teenager")
Reveal the correction
Put a colon at the end of the if line: if age >= 13:. The indentation then makes the following line part of the selection block.
2. Runtime error: user input is text
tickets = input("How many? ")
print(tickets + 2)
Reveal the correction
Convert a valid whole-number input before adding: tickets = int(input("How many? ")). For a real program, also handle unexpected text safely. The original code tries to combine a string with an integer.
3. Logic error: an off-by-one total
total = 0
for number in range(1, 5):
total += number
print(total)
The requirement is to add 1 through 5 inclusive. What does the existing code display?
Reveal the output and fix
It displays 10 because range(1, 5) stops before 5. Use range(1, 6) to include 5, giving a total of 15.
A practical debugging checklist
- Write the expected result for a normal case.
- Find the smallest input that reproduces the fault.
- Trace variables using a table or temporary printed values.
- Fix the underlying cause, not just the visible symptom.
- Rerun normal, boundary and invalid-input tests.
Further practice: step-by-step Python trace tables, interactive tracing and debugging, and 100 additional coding exercises.