Common Python Errors
Recognise and fix the errors you'll meet most
In this lesson
Python has a handful of errors you’ll hit again and again — SyntaxError, NameError, TypeError, IndexError, and more. Errors aren’t failures; they’re Python telling you exactly what it couldn’t do, so learning to recognise the common types (including syntax errors) means you’ll often fix a problem the moment you read its name.
Explain it like I’m 5
Each error type is a label on the problem. Once you know what the labels mean, the scary red text becomes a helpful sign pointing right at what to change.
Errors that stop the program before it runs: SyntaxError & IndentationError
A SyntaxError means the code isn’t valid Python — a missing colon, an unclosed bracket or quote. Python can’t even start running it. An IndentationError is a specific kind: the spacing of a block is wrong. Both are about the shape of the code, not its logic, and the reported line (or the one just before it) is where to look.
if True:
print("oops")
IndentationError: expected an indented block after 'if' statement on line 1
Name and type problems: NameError & TypeError
age = "17"
if int(age) >= 18:
print("Adult")
else:
print("Minor")
Minor
Value and lookup problems: ValueError, IndexError, KeyError
A ValueError means the type was right but the value was wrong — e.g. int("hello") can’t become a number. An IndexError means a list position doesn’t exist. A KeyError means a dictionary key isn’t there. These three are about asking for something that isn’t valid or present.
data = {"name": "Ada"}
# data["age"] would raise KeyError
print(data.get("age", "unknown"))
unknown
This raises a ValueError because the text isn't a whole number. Change the value so it converts and prints 42.
text = "forty-two"
number = int(text)
print(number)
int() only accepts text that looks like a whole number, e.g. "42" — not spelled-out words.
text = "42"
number = int(text)
print(number)
42
A repeatable approach
For any error: (1) read the last line for the type and message, (2) note the line number, (3) ask what the message is literally telling you, (4) make one targeted fix. The error name alone usually narrows it to a handful of causes you can check in seconds.
A few more you’ll meet
Beyond the core set, three others turn up regularly. An AttributeError means you used a method or attribute a value doesn’t have — like calling .append() on a string (strings have no such method). A ModuleNotFoundError means an import couldn’t find the module — usually a typo or a package that isn’t installed (modules and imports covers this). A RecursionError means a function kept calling itself with no stopping point.
The cure is always the same routine: read the last line, find the line number, and question the assumption behind it — exactly the process in debugging basics.
text = "hello"
text.append("!")
AttributeError: 'str' object has no attribute 'append'
Common mistake: Treating errors as dead ends instead of clues
Red error text feels like ‘you broke it’, so beginners stop reading and start guessing.
Read the error type and message as a hint. ‘NameError: name ‘prnt’ is not defined’ is literally telling you about a typo.
You wrote print(totl) but defined the variable as total. Which error?
An unknown name (here a typo) raises a NameError.
int("hello") raises which error?
The argument is the right type (a string) but the wrong value, so it’s a ValueError.
Mini exercise (medium)
Read each line and name the error it would raise: (a) print("hi" (b) x = [1, 2]; print(x[5]) (c) 5 + "5".
Don’t run the broken lines — instead, name the error each would raise. Set a, b, and c to the right error names as strings.
# (a) print("hi"
# (b) x = [1, 2]; print(x[5])
# (c) 5 + "5"
a = "???"
b = "???"
c = "???"
print(a, b, c)
Think: wrong shape? out-of-range position? mismatched types?
a = "SyntaxError"
b = "IndexError"
c = "TypeError"
print(a, b, c)
assert (a, b, c) == ("SyntaxError", "IndexError", "TypeError"), "the errors are SyntaxError, IndexError, then TypeError"
print("✓ All correct!")