There are three types of programming error tested at GCSE Computer Science: syntax errors, which break the grammar rules of the language; logic errors, where the program runs but produces the wrong output; and runtime errors, which cause the program to crash during execution. Knowing which type you are facing determines how you fix it.
What is a syntax error?
A syntax error occurs when code violates the grammar rules of the programming language. Just as a sentence "Cat the sat mat on the" violates the grammar of English, if x = 5 violates Python's grammar (you need ==, not =, inside an if condition).
Key characteristics:
- Detected by the interpreter or compiler before the program runs.
- The program will not run at all until all syntax errors are fixed.
- The IDE usually highlights the error and states the line number.
Common Python syntax errors:
| Error | Incorrect code | Correct code |
|---|---|---|
Missing colon after if/for/while/def |
if x > 5 |
if x > 5: |
Using = instead of == in a condition |
if x = 5: |
if x == 5: |
| Unclosed bracket or quote | print("Hello) |
print("Hello") |
| Wrong indentation | def greet(): ← print("Hi") (same level) |
Body indented by 4 spaces |
| Misspelling a keyword | whlie True: |
while True: |
Example — Python error message for a missing colon:
File "program.py", line 3
if x > 5
^
SyntaxError: expected ':'
The caret (^) points directly to the problem. Syntax errors are the easiest type to locate because the IDE tells you exactly where they are.
What is a logic error?
A logic error occurs when the program runs without crashing but produces the wrong output because the programmer's reasoning was flawed. The code is grammatically correct — it is the algorithm that is wrong.
Logic errors are the hardest type to find because there is no error message. The program appears to work; only the output betrays the problem.
Examples of logic errors:
| Intended behaviour | Flawed code | Correct code |
|---|---|---|
| Check if x is greater than 10 | if x >= 10: |
if x > 10: |
| Sum numbers 1 to n | total = 0 then for i in range(n): |
for i in range(1, n+1): — range(n) starts at 0 |
| Average of two numbers | average = a + b / 2 |
average = (a + b) / 2 — missing parentheses |
| Count down from n to 1 | while n >= 0: |
while n >= 1: — goes one step too far |
Worked example — off-by-one logic error:
# Intended: print numbers 1 to 5
for i in range(5): # Logic error: range(5) gives 0, 1, 2, 3, 4
print(i)
Output: 0 1 2 3 4 — wrong; should be 1 2 3 4 5.
# Corrected:
for i in range(1, 6): # range(1, 6) gives 1, 2, 3, 4, 5
print(i)
The off-by-one error — producing n items when n+1 was needed, or starting from 0 instead of 1 — is one of the most frequent logic errors in computing at all levels.
What is a runtime error?
A runtime error (also called an exception) occurs while the program is running, after it has passed the syntax check. The code is grammatically correct, but the program encounters an impossible operation during execution and crashes.
Characteristics:
- The program starts running successfully.
- At the moment the problematic line executes, Python stops and prints a traceback (crash report).
- The error message identifies the exception type, line number, and a description.
Common Python runtime errors:
| Exception | Cause | Example trigger |
|---|---|---|
ZeroDivisionError |
Dividing by zero | result = 10 / 0 |
IndexError |
Accessing a list element that does not exist | my_list[5] when list has 3 items |
ValueError |
Correct type, wrong value | int("hello") |
TypeError |
Operation on incompatible types | "5" + 5 |
NameError |
Using a variable that has not been defined | print(total) before total is assigned |
FileNotFoundError |
Opening a file that does not exist | open("missing.txt") |
Example runtime error message:
Traceback (most recent call last):
File "quiz.py", line 7, in <module>
answer = scores[5]
IndexError: list index out of range
The traceback tells you: file name, line number, the offending line, and the exception type.
How do the three error types compare?
| Feature | Syntax error | Logic error | Runtime error |
|---|---|---|---|
| When detected | Before running | Only when outputs are checked | During execution |
| Does program run? | No | Yes | Starts, then crashes |
| Is there an error message? | Yes — from interpreter | No — wrong output, no message | Yes — traceback with type and line |
| How to fix | Correct grammar/spelling in code | Rethink algorithm; use trace table or debugger | Add exception handling or fix the logic |
| How hard to find? | Easy — IDE highlights it | Hard — requires testing and debugging | Medium — traceback gives the location |
How do you identify which type of error you have?
Use this decision process:
- Does the program refuse to start? → Syntax error. Read the error message and fix the grammar on the indicated line.
- Does the program run and then crash mid-way? → Runtime error. Read the traceback — it gives you the exception type and line number. Fix the logic or add exception handling.
- Does the program run to completion but give wrong output? → Logic error. Add
printstatements or use a trace table to follow the values of key variables step by step until you find where they deviate from what you expected.
Frequently asked questions
What is the most common error for beginners in Python?
At beginner level, syntax errors are most frequent — especially missing colons after if, for, while, and def statements, and forgetting to indent the body of a code block. Python's error messages are reasonably helpful about location, but beginners often need to read them carefully. After syntax errors, off-by-one logic errors in loops and range arguments are the next most common problem.
Can a program have more than one type of error at the same time?
Yes. A program might have a syntax error that prevents it running, and once that is fixed, a runtime error may appear, and once that is handled, a logic error in the output may still remain. Fixing one type of error does not guarantee the others are absent. Thorough testing — using normal, boundary, and erroneous test data — is needed to check for all three types.
How do you fix a logic error?
Logic errors require reasoning about the algorithm, not just the syntax. Useful strategies include: using a trace table to track variable values step by step; adding temporary print statements at key points to inspect values mid-execution; using a debugger to step through code in slow motion; and checking loop ranges and conditional operators carefully, paying particular attention to > vs >= and range(n) vs range(1, n+1). Identifying the exact line where values first deviate from expectations is the key diagnostic step.
Is a runtime error the same as a logic error?
No. A runtime error causes the program to crash with an error message during execution (e.g. dividing by zero raises ZeroDivisionError). A logic error allows the program to run to completion but produces wrong output with no error message at all. Both occur during execution, but a runtime error announces itself loudly; a logic error is silent — the only symptom is that the output is incorrect. This makes logic errors considerably harder to locate than runtime errors.
For Socratic GCSE Computer Science coaching on debugging, programming errors, and Python, visit aitutors.me.