A syntax error stops the program from starting. A runtime error stops it part-way through.
A logic error lets it finish with the wrong answer. Knowing which one you have tells you where to look.
This lesson follows testing indentation and branch structure in Python transition and execution.
How do the three kinds of fault differ?
| Fault | When it shows | What you see | How to find it |
|---|---|---|---|
| Syntax error | Before anything runs | Error message and line number | Read the line, check colons, brackets, quotes, = versus == |
| Runtime error | While running | Program stops with a message | Read the message, trace to that line |
| Logic error | After finishing | A wrong result, no message | Test with known answers, trace the working |
Syntax and runtime errors announce themselves. A logic error is silent, so it is the one that costs marks when you only check “did it run?”.
Worked example
Task: find the average of three numbers. Test with 6, 9 and 12, where the correct average is (6 + 9 + 12) / 3 = 27 / 3 = 9.
def average(a, b, c):
return a + b + c / 3
print(average(6, 9, 12))
Step 1, does it run? Yes. There is no syntax or runtime error, so the fault, if any, is in the logic.
Step 2, trace the expression. Division is done before addition, so Python works out c / 3 first: 12 / 3 = 4. Then 6 + 9 + 4 = 19.
Step 3, compare. 19 is not 9, so this is a logic error.
Step 4, repair with brackets.
def average(a, b, c):
return (a + b + c) / 3
Now (6 + 9 + 12) / 3 = 9.0. Python shows 9.0 because / always gives a decimal result.
The mistake to watch for
A common slip is to assume that a program with no error message is correct.
Mistaken answer: “I ran it and it printed 19, so it works.”
The program ran, but nobody compared 19 with the expected 9. A check against a known answer is what exposes a logic error.
The correction is to work out the expected answer by hand before you run the code. Then the comparison tells you whether the fault is in the code or in your reading of the question.
A true syntax error looks different. In if mark = 50: the single = is an assignment where a comparison belongs, so Python refuses to run it and reports a syntax error. The fix is if mark == 50:.
Check yourself
1. What kind of error is print("Hi", with no closing bracket?
Show answer
A syntax error. The bracket is never closed, so Python cannot read the line and the program does not start.
2. This function should return the total of a list. Which kind of fault does it have when called with [2, 4]?
def total(values):
t = 0
for n in values:
t = n
return t
Show answer
A logic error. It runs, but t = n replaces the total each time instead of adding, so it returns 4, not 6. The line should be t = t + n.
3. What kind of error is print(10 / 0)?
Show answer
A runtime error. The code is valid, but dividing by zero is impossible, so the program stops with a ZeroDivisionError message.
Where this leads next
Next, use a safe browser runtime to run these examples without risk. You can test the average function in the Python reasoning sandbox and compare it with the restricted pseudocode trace trainer.
If a wrong answer keeps surviving your own checks, our online one-to-one Computer Science tuition can work on testing habits with you.