Common Python Errors

Recognise and fix the errors you'll meet most

Beginner 10 min

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.

Example
if True:
print("oops")
Output
IndentationError: expected an indented block after 'if' statement on line 1
The body of the if isn't indented.

Name and type problems: NameError & TypeError

A NameError means you used a name Python doesn’t recognise — usually a typo or a variable used before it’s defined. A TypeError means you used a value in a way its type doesn’t allow, like adding a string to a number ("3" + 5). Read the message: it names the variable or the types involved.

Example
age = "17"
if int(age) >= 18:
    print("Adult")
else:
    print("Minor")
Output
Minor
Fixing a TypeError by converting the value first.

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.

Example
data = {"name": "Ada"}
# data["age"] would raise KeyError
print(data.get("age", "unknown"))
Output
unknown
Avoiding a KeyError with .get().

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)

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.

Example
text = "hello"
text.append("!")
Output
AttributeError: 'str' object has no attribute 'append'
The message names the type and the missing method.

Common mistake: Treating errors as dead ends instead of clues

Why it happens:

Red error text feels like ‘you broke it’, so beginners stop reading and start guessing.

How to fix it:

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?

int("hello") raises which error?

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)

What to learn next

You learned to recognise the everyday errors, SyntaxError, NameError, TypeError, ValueError, IndexError, KeyError, and IndentationError, and how to fix each quickly. You practised by naming the exact error that three broken snippets would raise.

Recognising errors is half the battle; the other half is handling them gracefully instead of crashing. Exceptions and try/except shows how to catch specific exceptions, use else and finally, and raise your own. When you’re ready, continue to the next lesson.