Every programmer sees errors every day. They're Python telling you exactly which step it didn't understand. This page shows how to read them, and what the ones you've met so far mean.
1. How to read an error
When something goes wrong, Python shows a few lines. You only need two things:
The last lineSays what went wrong. For example: NameError: name 'Namaste' is not defined.
The line numberSays where. For example: line 1. Sometimes a little ^ arrow points at the exact spot.
Start with the last line. Fix one thing. Run again.
Two kinds of stop. If something is written wrong (a missing bracket, curly quotes), Python refuses to start at all, and nothing runs. If it's written fine but something goes wrong while running (like a name it doesn't know), the lines before it have already run, and it stops at that line.
Which part of an error should you read first?
2. Errors that always mean one thing
These have just one cause. When you see one, you know exactly what to fix.
A bracket was opened and never closed
print("Namaste"
SyntaxError: '(' was never closed
Add the missing ).
A closing bracket has no opening one
print("Namaste"))
SyntaxError: unmatched ')'
Remove it, or add the missing (.
Curly "smart" quotes, from a phone or Word
print(“Namaste”)
SyntaxError: invalid character '“' (U+201C)
Retype the quotes from your keyboard.
A text starts with a quote but never ends
print("Namaste)
SyntaxError: unterminated string literal (detected at line 1)
Add the closing quote, the same kind.
print without brackets
print "hi"
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
Add brackets: print("hi").
The name and the value are the wrong way round
"Alex" = name
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Name on the left: name = "Alex".
You see "invalid character '“'". What happened?
3. NameError: a few usual causes
Python looked for a name it doesn't know. Check these, in this order:
Forgot the quotes?
print(Namaste)
NameError: name 'Namaste' is not defined
Should be print("Namaste"). Without quotes, Python thinks it's a name.
Spelling?
name = "Alex"
print(nmae)
NameError: name 'nmae' is not defined
You made name but wrote nmae. Python often adds a hint like Did you mean: 'name'?
Capitals?
done = true
NameError: name 'true' is not defined
true should be True. Python cares about capital letters.
Used before it was made?
print(city)
city = "Oslo"
NameError: name 'city' is not defined
The line that creates it must come before the line that uses it.
You see NameError: name 'Ravi' is not defined, from print(Ravi). Most likely?
4. SyntaxError: invalid syntax
Python can't read the line at all. Look near the ^ arrow for a space inside a name, or a missing quote, bracket or comma.
A space inside a name
customer name = "Alex"
SyntaxError: invalid syntax
customer name should be customer_name.
5. Try it: fix three bugs
FIX IT SO IT PRINTS NAMASTE
OUTPUT
Press Run
FIX IT SO IT PRINTS LATTE
OUTPUT
Press Run
FIX IT SO IT PRINTS OSLO
OUTPUT
Press Run
Back to workKeep this page handy: every new kind of error you meet gets added here.