Every Python beginner meets the same ten or so errors, over and over. Once you know what each message means, most take seconds to fix. This guide lists the Python errors beginners make most, with an example of each, what the message is telling you, and the fix.
First: how to read an error message
A Python error — called a traceback — looks alarming, but it has a simple structure. Read it from the bottom up:
Traceback (most recent call last):
File "shop.py", line 3, in <module>
total = price * quantity
NameError: name 'quantity' is not defined
- The last line names the error type and describes the problem:
NameError, and the name it could not find. - The line above shows the code where it happened, and the line number.
That is usually all you need. The line number points to where Python noticed the problem, which is often — but not always — where the mistake is. If the line looks fine, check the line before it.
The ten most common beginner errors
1. SyntaxError: missing colon
if age > 18
print("Adult")
What it means: Python could not understand the line's grammar. Lines starting if, elif, else, for, while and def must end with a colon.
Fix: if age > 18:
2. IndentationError
if age > 18:
print("Adult")
What it means: in Python, indentation is part of the language. The code that belongs to an if, loop or function must be indented, and consistently.
Fix: indent the line by four spaces. Also avoid mixing tabs and spaces; most editors can convert tabs to spaces automatically.
3. NameError: name '…' is not defined
print(mesage)
What it means: you used a name Python has not seen. Usually a typo, a variable used before it is created, or a missing quotation mark around text.
Fix: check spelling (message), make sure the variable is assigned above this line, or add quotes if you meant text: print("message").
4. TypeError: can only concatenate str (not "int") to str
age = 30
print("You are " + age + " years old")
What it means: you tried to join text and a number with +. Python will not guess which you meant.
Fix: use an f-string, which handles the conversion for you: print(f"You are {age} years old").
5. The input() trap (a TypeError, or wrong answers)
a = input("First number: ")
b = input("Second number: ")
print(a + b)
What goes wrong: type 2 and 3 and this prints 23. input() always returns text, so + joins the strings instead of adding numbers.
Fix: convert first: a = int(input("First number: ")) — or float() for decimals.
6. ValueError: invalid literal for int()
n = int(input("Number: ")) # user types "five"
What it means: the value has the right type but cannot be converted — "five" is text, not digits.
Fix: handle it with try/except:
try:
n = int(input("Number: "))
except ValueError:
print("Please type digits, like 5.")
7. IndexError: list index out of range
colours = ["red", "green", "blue"]
print(colours[3])
What it means: you asked for a position that does not exist. Lists count from 0, so a three-item list has positions 0, 1 and 2.
Fix: use colours[2] for the last item, or colours[-1], which always means "last". Our guide to Python lists explains indexing in full.
8. KeyError
prices = {"apple": 0.5, "pear": 0.6}
print(prices["banana"])
What it means: the dictionary has no such key.
Fix: check first with if "banana" in prices:, or use prices.get("banana"), which returns None instead of crashing. See Python dictionaries explained.
9. Using = instead of ==
if answer = "yes":
print("Great")
What it means: a single = assigns a value; a double == compares. Python reports a SyntaxError here.
Fix: if answer == "yes":
10. TypeError: 'NoneType' object is not subscriptable
names = ["Sam", "Ana"]
names = names.sort()
print(names[0])
What goes wrong: .sort() sorts the list in place and returns None. Assigning its result replaced your list with None, and None has no positions to index. The same mistake with a method call instead gives AttributeError: 'NoneType' object has no attribute ….
Fix: either call names.sort() on its own line, or write names = sorted(names).
Errors that do not show an error
The hardest bugs produce no message at all — just a wrong answer. Two classics:
- Off-by-one loops.
range(1, 10)stops at 9, not 10. Userange(1, 11)to include 10. - Code outside the loop that should be inside, or the reverse — an indentation problem that is valid Python but wrong logic.
For these, add print() calls to see what your variables hold at each step, or step through the program line by line in an editor such as Thonny.
A debugging routine that always helps
- Read the last line of the error, then the line number.
- Look at that line and the one above it.
- Print the values involved to see what Python actually has.
- Shrink the problem: comment out code until the error disappears, then bring it back piece by piece.
- Still stuck? Paste the code and full error into an AI assistant, but ask it to explain the error and give a hint — not to fix it. Our guide on learning Python with ChatGPT has the exact prompt.
Practising with short drills is the fastest way to stop making these; try our Python exercises for beginners.
Python for Beginners covers error handling with try/except in its part on functions, and the free Practice Toolkit on the Python for Beginners page includes a one-page common-mistakes checklist — indentation, missing colons, = versus ==, input() giving you text, off-by-one ranges — to run through before you start doubting yourself.
Frequently asked questions
Why does Python say the error is on the wrong line?
Python reports where it noticed the problem. A missing bracket or quote on one line is often only noticed on the next, so always check the line above too.
Are errors a sign I am bad at programming?
No. Professional programmers see error messages all day. The skill is reading them quickly, which comes with practice.
What is the difference between an error and an exception?
In everyday Python, the words are used almost interchangeably. Exceptions are errors raised while the program runs, and you can handle them with try/except.