7. Reading Error Messages¶
When Python can't run a line, it stops and prints a traceback instead of quietly failing. It looks scary at first, but it's actually telling you exactly what to fix.
print(player_name)
Traceback (most recent call last):
File "game.py", line 1, in <module>
print(player_name)
NameError: name 'player_name' is not defined
Read it from the bottom up:
- Last line — the actual problem: a
NameErrorbecauseplayer_namewas never created. - The lines above show where it happened: file
game.py, line 1.
A few errors you'll see constantly:
| Error | Usually means |
|---|---|
NameError |
You used a variable or function name that doesn't exist (often a typo) |
SyntaxError |
Python can't parse the line — a missing colon, bracket, or quote |
IndentationError |
Lines that should line up don't |
TypeError |
You mixed types that don't work together, like "3" + 3 |
ZeroDivisionError |
You divided by zero |
Try it
Run print(undefined_variable) on purpose, read the traceback, and identify which part tells you the error type versus which part tells you where it happened.
Tip
Read the last line first. It usually names the actual problem; everything above it is just the trail of how you got there.