The most common Python errors are SyntaxError, IndentationError, NameError, TypeError, ValueError, IndexError and KeyError. The last line of an error message names the error type and the reason, so read that line first. This page shows each message exactly as Python prints it, what causes it, and how to fix it.
How do you read a Python error message?
Traceback (most recent call last): File "cafe.py", line 2, in <module> print(totl) NameError: name 'totl' is not defined. Did you mean: 'total'?
- Last line: the error type (
NameError) and the reason. - Line above it: the code that failed.
- `line 2`: the line number.
Read from the bottom up. Python sometimes suggests a fix, like Did you mean: 'total'? here.
SyntaxError: '(' was never closed
print("Corner Café"
SyntaxError: '(' was never closedA syntax error means the code breaks a writing rule of Python. Python reads the whole file before it runs anything, so it stops here. An unclosed bracket, a missing quote or a missing colon all cause it. Sometimes the real mistake is on the line above the one Python points to.
SyntaxError: expected ':'
if True print("open")
SyntaxError: expected ':'Lines like if, for, while and def must end with a colon. Add it and the error goes away.
IndentationError: expected an indented block
for table in range(3): print(table)
IndentationError: expected an indented block after 'for' statement on line 1Python uses indentation to see where a block starts. The lines after for, if or def must be indented, and the usual size is 4 spaces. The related message IndentationError: unexpected indent appears when you indent a line that should not be indented. Do not mix tabs and spaces in the same file.
NameError: name is not defined
total = 12.5
print(totl)NameError: name 'totl' is not defined. Did you mean: 'total'?
Python does not know the name you used. The cause is usually a typo. It can also mean you used the variable before you created it. Python is case sensitive, so Total and total are different names.
TypeError: can only concatenate str (not "int") to str
quantity = 3 print("Quantity: " + quantity)
TypeError: can only concatenate str (not "int") to str
You cannot join a string and an integer with +. Use an f-string, or convert with str():
quantity = 3 print(f"Quantity: {quantity}")
A type error also appears when you call a function with the wrong number of arguments:
def add(a, b): return a + b add(1)
TypeError: add() missing 1 required positional argument: 'b'ValueError: invalid literal for int()
quantity = int("three")
ValueError: invalid literal for int() with base 10: 'three'
The type is right (int() accepts a string), but the value is wrong: "three" is not made of digits. This is common with user input. Check the input first, or catch the error with try and except. The same idea gives ValueError: could not convert string to float: '4,25' when a price uses a comma instead of a decimal point.
IndexError: list index out of range
menu = ["tea", "latte", "cookie"] print(menu[3])
IndexError: list index out of range
A list with three items has positions 0, 1 and 2. Python starts counting at 0. Get the last item with menu[2] or menu[-1].
KeyError: 'Tea'
prices = {"tea": 1.25}
print(prices["Tea"])KeyError: 'Tea'The message shows only the key that was not found. Keys must match exactly, including capital letters. If the key might be missing, use get:
prices = {"tea": 1.25}
print(prices.get("Tea"))
print(prices.get("Tea", 0))get returns None when the key is missing, or the default value you give as the second argument.
Quick reference
| Error | Meaning | Check first |
|---|---|---|
SyntaxError | A writing rule is broken | Brackets, quotes, colons |
IndentationError | Wrong indentation | Alignment of the block |
NameError | Name not defined | Spelling, order |
TypeError | Types do not fit | String + number, argument count |
ValueError | Value cannot be converted | User input |
IndexError | Position does not exist | List length, counting from 0 |
KeyError | Key does not exist | Spelling, capital letters |
What should you do when you get an error?
Getting errors is normal. Experienced programmers see them every day and fix them fast. Follow these steps:
1. Read the last line of the message.
2. Go to the line number. If that line looks fine, check the line above it.
3. Print the variable you suspect, right before the failing line. Its value or type is often not what you expected.
4. If you still do not understand the message, search for the last line exactly as Python printed it.
What to read next
- Practice reading messages in the lesson [Error messages and getting help](/learn/python/error-messages-help) and the lesson [Error types and the traceback](/learn/python/error-types-traceback).
- Catch errors inside your program with [Python try except](/blog/python-try-except).
- Learn the lessons behind these errors in the [Python course](/courses/python).
- #python
- #errors
- #debugging