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.
Three kinds of error
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
| Error name | Usual cause | Example |
|---|---|---|
| SyntaxError | Missing colon, bracket or quote | if score > 5 (no colon) |
| IndentationError | Lines inside a block not indented | print(i) not indented under for |
| NameError | Variable misspelt or used before it is given a value | print(totl) |
| TypeError | Mixing data types that cannot be combined | "15" + 1 |
| ValueError | Converting text that is not a number | int("ten") |
| IndexError | List index out of range | days[3] when there are 3 items |
| ZeroDivisionError | Dividing by zero | total / 0 |
Hunting a logic error
# 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
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.
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?
- It runs without crashing, but prints 1, 2, 3, 4: the 5 is missing.
- range(1, 5) stops before 5, so it is a logic error.
- Change it to range(1, 6).
Answer: Logic error: change range(1, 5) to range(1, 6).
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.
- 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 →
Try two quick questions
See if it has sunk in. Answers are marked straight away.
2 more questions
Try each one, then tap to check.
What type of error does this code contain?
for i in range(3)
print(i)- Syntax error
- Runtime error
- Logic error
- No error
Show the answer
Answer: Syntax error
The for line is missing its colon: SyntaxError.
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.
- Pearson Edexcel GCSE Computer Science (1CP2) specification, topic 1.2.5, 6.1.5
- Pearson Edexcel International GCSE Computer Science (4CP0) specification, topic 2.1.3, 2.1.5
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? →