How to read a traceback
Every Python error prints a traceback. Read the last line first: it names the error type and gives a message. Then look at the line above it for the file and line number. The lines further up show the chain of function calls that led there. In the Coding Python app the console highlights the error type, so this habit transfers directly.
Syntax and indentation errors
1. SyntaxError: expected ':'
if x > 3
print(x)Fix: every if, elif, else, for, while, def, class, try and with line ends with a colon.
2. SyntaxError: '(' was never closed
print("total:", sum([1, 2, 3])Fix: count your brackets. Modern Python (3.10+) points at the unclosed one. Editors with bracket matching, including the app's, help.
3. SyntaxError: invalid syntax — using = instead of ==
if score = 100:
print("perfect")Fix: = assigns, == compares. Use if score == 100:.
4. IndentationError: expected an indented block
for i in range(3):
print(i)Fix: the body of a block must be indented (four spaces). unindent does not match any outer indentation level means you mixed tabs and spaces or lined up inconsistently.
5. SyntaxError: Missing parentheses in call to 'print'
print "hello"Fix: that is Python 2 syntax. Write print("hello"). The tutorial you copied it from is out of date.
Name and attribute errors
6. NameError: name 'x' is not defined
total = 10
print(totl)Fix: usually a typo, or you used the variable before assigning it, or it was assigned inside a function and you are outside. Check spelling and order.
7. AttributeError: 'list' object has no attribute 'push'
items = []
items.push(1)Fix: the method does not exist on that type. Lists use append. Run dir(items) or help(list) to see what is available. If the message says 'NoneType' object has no attribute…, a function returned None — commonly lst = lst.sort().
8. ModuleNotFoundError: No module named 'requests'
Fix: the package is not installed (pip install requests) or the name is misspelled. In the Coding Python app only the standard library is available — check the standard library lesson for alternatives.
Type and value errors
9. TypeError: can only concatenate str (not "int") to str
age = 30
print("Age: " + age)Fix: convert or format: print(f"Age: {age}") or "Age: " + str(age). — Strings
10. TypeError: 'int' object is not iterable
for i in 5:
print(i)Fix: loop over range(5). The same message with 'NoneType' means you are looping over a function result that was None.
11. TypeError: greet() missing 1 required positional argument: 'name'
Fix: the function expects an argument you did not pass. Check the definition; give the parameter a default if it is optional. — Functions
12. ValueError: invalid literal for int() with base 10: 'abc'
n = int(input("Number: ")) # user typed abcFix: wrap in try/except ValueError and ask again. — Exceptions
Index and key errors
13. IndexError: list index out of range
nums = [1, 2, 3]
print(nums[3])Fix: indexes start at 0, so the last item is nums[len(nums) - 1] or nums[-1]. Loops that use range(len(x) + 1) cause this too.
14. KeyError: 'email'
user = {"name": "Ada"}
print(user["email"])Fix: use user.get("email", "n/a") or check if "email" in user. — Dictionaries
15. ZeroDivisionError: division by zero
average = total / count # count is 0Fix: guard it: average = total / count if count else 0.
Bonus: bugs that raise no error
- Infinite loop — the
whilecondition never becomes false. Stop the program and make sure something inside the loop changes it. - Integer division surprise —
5 / 2is2.5,5 // 2is2. - Float comparison —
0.1 + 0.2 == 0.3is False. Compare with a tolerance or useround(). - Shadowing a built-in — naming a variable
list,strorsumbreaks the built-in for the rest of the program. - Mutable default argument —
def f(x=[])shares one list across calls.
Every one of these is easier to spot with fast feedback, which is a good argument for running code often while you write it.
Frequently asked questions
What is the most common Python error for beginners?
SyntaxError from a missing colon or bracket, closely followed by IndentationError and NameError from typos. All three are caught quickly once you learn to read the last line of the traceback.
How do I fix IndentationError in Python?
Use exactly four spaces for each level and never mix tabs and spaces. Most editors, including the Coding Python app, can convert tabs to spaces automatically.
What is the difference between TypeError and ValueError?
TypeError means the wrong kind of object was used (adding a string to an int). ValueError means the type is right but the value is not acceptable (int('abc')).
Why does my program say NoneType has no attribute?
A function returned None where you expected an object, often because list.sort(), list.append() or a function without return was assigned to a variable.