Finding and fixing errors in Python

Every programmer writes bugs. What matters is finding them fast. Python's error messages tell you the type of error and the line, and testing finds the errors that give no message at all.

Practise this lesson free (7 questions)

Three kinds of error

Syntax error: the code breaks the rules of the language, so it cannot run at all. Examples: a missing colon, bracket or quote, or a misspelt keyword.
Runtime error: the code starts, but crashes while running. Examples: dividing by zero, using a list index that does not exist, or turning "ten" into an integer.
Logic error: the program runs and does not crash, but gives the wrong result. Python gives no message, so you find these by testing.

Reading Python's error messages

The last line of the message names the error. Look at the line number it gives, and also the line just before it: a missing bracket is often reported on the next line.
Error nameUsual causeExample
SyntaxErrorMissing colon, bracket or quoteif score > 5 (no colon)
IndentationErrorLines inside a block not indentedprint(i) not indented under for
NameErrorVariable misspelt or used before it is given a valueprint(totl)
TypeErrorMixing data types that cannot be combined"15" + 1
ValueErrorConverting text that is not a numberint("ten")
IndexErrorList index out of rangedays[3] when there are 3 items
ZeroDivisionErrorDividing by zerototal / 0

Hunting a logic error

This program should count marks of 50 or more, but prints 2 instead of 4:
# Count the passes: a mark of 50 or more is a pass
marks = [72, 50, 38, 50, 91]
passes = 0
for mark in marks:
if mark > 50:
passes = passes + 1
print(passes)
The two marks of exactly 50 are missed because the condition uses > instead of >=. Testing with boundary data (exactly 50) finds this kind of error.
Common logic errors: the wrong comparison operator, a loop that runs one time too many or too few, a variable reset inside a loop, and a calculation done inside a loop instead of after it.

Debugging methods

Read the error message and go to the line.
Use a trace table to follow the values of the variables.
Add temporary print() lines to show values while the program runs.
Test with normal, boundary and erroneous data, and compare the actual output with the expected output.
Worked example

This program should print the numbers 1 to 5:
# Print the numbers 1 to 5
for n in range(1, 5):
print(n)
What type of error is there, and how do you fix it?

  1. It runs without crashing, but prints 1, 2, 3, 4: the 5 is missing.
  2. range(1, 5) stops before 5, so it is a logic error.
  3. Change it to range(1, 6).

Answer: Logic error: change range(1, 5) to range(1, 6).

Key idea

Syntax errors stop code running; runtime errors crash it while it runs; logic errors give wrong results without any message. Read the error name and line number, then trace and test to find logic errors.

Key words
Syntax error
Code that breaks the rules of the language, so it cannot be translated or run.
Runtime error
An error that makes a program crash while it is running, such as dividing by zero.
Logic error
An error that lets a program run but gives the wrong result.
Boundary data
Valid test data at the very edge of the allowed range.
Variable
A named store whose value can change while the program runs.
Trace table
A table that tracks the value of each variable as an algorithm runs.

All GCSE and International GCSE Computer Science key terms and definitions →

Quick check

Try two quick questions

See if it has sunk in. Answers are marked straight away.

Extra practice

2 more questions

Try each one, then tap to check.

  1. What type of error does this code contain?
    for i in range(3)
    print(i)

    1. Syntax error
    2. Runtime error
    3. Logic error
    4. No error
    Show the answer

    Answer: Syntax error

    The for line is missing its colon: SyntaxError.

  2. Which error name does Python give when this runs?
    score = 10
    score = score + 5
    print(Score)

    Show the answer

    Answer: NameError

    Score (capital S) was never created. Python is case sensitive, so it raises a NameError.

Check you have got it

Answer 7 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.

Video for this lesson coming soon. Subscribe on YouTube

Sources and references

Checked by the GCSE-ready editorial team, 17 October 2026. Last updated 2 October 2026. Spotted a mistake? Tell us and we will fix it.

What do exam command words like explain and evaluate mean? →

Related lessons