Skip to content
IGCSE·Tuition
Computer Science · Lessons

Distinguish a syntax error from a wrong result

Red text on the screen feels worse than a wrong answer, yet it is usually the easier fault to fix.

On this page
  1. How do the three kinds of fault differ?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

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?

FaultWhen it showsWhat you seeHow to find it
Syntax errorBefore anything runsError message and line numberRead the line, check colons, brackets, quotes, = versus ==
Runtime errorWhile runningProgram stops with a messageRead the message, trace to that line
Logic errorAfter finishingA wrong result, no messageTest 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.

Questions people ask

What is a syntax error?

A syntax error means the code breaks the rules of the language, such as a missing colon or an unclosed bracket. The program cannot start at all. Python usually points to the line, or just after it, where it got confused.

What is a logic error?

A logic error is code that runs without complaint but gives a wrong result because the method is wrong. Python cannot detect it, because the code is valid. You find it by testing with known answers and tracing.

What is a runtime error?

A runtime error happens while the program is running, for example dividing by zero or using a variable that was never given a value. The program starts, then stops at that line with an error message.

Updated:

Your next step

If you are never sure whether a fault is in the typing or in the thinking, a one-to-one teacher can sort your errors with you and show a method for each kind.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service