KS3 · Python Programming

Python debugging challenge

Free worked examples, independent practice and self-check answers.

Read the error, find the cause

Debugging means systematically locating and correcting defects. Syntax errors prevent parsing; runtime errors arise while a program runs; logic errors produce incorrect results.

scores = [12, 15, 9]
total = 0
for score in scores:
    total = score
mean = total / len(scores)
print(mean)

The program runs but the output is incorrect. Fix the logic error without using sum().

Suggested solution
scores = [12, 15, 9]
total = 0
for score in scores:
    total += score
mean = total / len(scores)
print(mean)  # 12.0

Extension

Adapt the program to deal safely with an empty list.

Debugging as a repeatable process

Reproduce the error, read the message or incorrect output, and isolate the smallest piece of code that demonstrates it. Trace the relevant variables and compare actual behaviour with the expected result. Change one thing, then rerun the original test.

Try our Python tracing and debugging challenges for short exercises with immediate feedback.

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

  1. Write the expected result for a normal case.
  2. Find the smallest input that reproduces the fault.
  3. Trace variables using a table or temporary printed values.
  4. Fix the underlying cause, not just the visible symptom.
  5. Rerun normal, boundary and invalid-input tests.

Further practice: step-by-step Python trace tables, interactive tracing and debugging, and 100 additional coding exercises.

Test the fix

A useful debugging checklist for students

Identify whether the problem is syntax, runtime or logic; write down the expected result; make one change at a time; then run a normal, boundary and erroneous test where appropriate.

Original Python debugging practice

Test the edge case before calling a bug fixed

A program can run without crashing and still give the wrong result. This small example confuses an inclusive condition with an exclusive one.

Bug: a student qualifies with exactly five marks

score = 5
if score > 5:
    print("Qualified")
else:
    print("Not qualified")

Expected: a score of five or higher should qualify. Test scores 4, 5 and 6 and record the actual output for each.

Reveal the fix and three useful tests

Replace score > 5 with score >= 5. Scores 4, 5 and 6 should print Not qualified, Qualified and Qualified respectively. This is a logic error: the original Python syntax was valid.

Next: interactive code tracing, 100 extra Python exercises, or GCSE Python revision.