24 min read
Types of Errors
Syntax errors, runtime errors and logic errors — how to recognise each, with Python examples you can run.
Three Types of Error
| Programming errors | ||
|---|---|---|
Syntax Errors
Missing bracket — press RunPython
total = 450 + 280print("Total:", totalPython reports SyntaxError and points near the problem. Other common syntax errors: missing colon after if, while, for or def; wrong indentation; mismatched quotes; using a keyword as a variable name.
Runtime Errors
Dividing by zeroPython
items = 0total = 1500average = total / itemsprint(average)| Common Python runtime errors | ||
|---|---|---|
"Undeclared" or "Undefined Identifier" Errors
An identifier is a name you give to a variable or function. Using a name that was never created — often a typing mistake like totl instead of total — is called an undeclared or undefined identifier.
- In compiled languages such as C, C++ and Java, variables must be declared before use. The compiler reports "undeclared identifier" before the program runs — it is treated as a compile-time (syntax) error.
- In Python, variables are created when first assigned, so the same mistake is only discovered when that line runs: a NameError — a runtime error.
An undefined identifier in PythonPython
total = 730print("Total:", totl)Logic Errors
Spot the logic errorPython
mark1 = 60mark2 = 80average = mark1 + mark2 / 2print("Average:", average)Expected output
Average: 100.0
The average of 60 and 80 should be 70, but the program prints 100.0. Division happens before addition, so it calculated 60 + 40. The fix is brackets: (mark1 + mark2) / 2. Logic errors are the hardest to find — only testing with known answers reveals them.
Practice
Choose all that apply.
More lessons in Introduction to Programming · Next: Structured Programming · Previous: The Programming Process
