Level 1
0 / 100 XP

Common errors

When your code breaks, Python prints a traceback — a block of red text that looks intimidating but is actually a map straight to the problem. Learn to read it and most bugs become obvious.

Read the traceback from the bottom up

The last line is the most important. It tells you the error type and a short message. The lines above it show where in your code the error happened.

Python
print(total)
Python
Traceback (most recent call last): File "main.py", line 1, in <module> print(total) NameError: name 'total' is not defined

Start at the bottom: NameError: name 'total' is not defined. That tells you exactly what's wrong. The line above shows the offending code and its line number. Always read the last line first.

NameError — a name Python doesn't know

A NameError means you used a name that hasn't been defined — usually a typo or a variable you forgot to create:

Python
message = "Hello" print(mesage) # NameError: name 'mesage' is not defined

The fix: check your spelling and make sure the variable is defined before you use it.

TypeError — the wrong type for the operation

A TypeError happens when you try to do something with a value that its type doesn't support — like adding a string and a number:

Python
age = 25 print("Age: " + age) # TypeError: can only concatenate str (not "int") to str

You can't glue a string and an int together with +. The fix is to convert the number, or use an f-string:

Python
age = 25 print("Age: " + str(age)) # Age: 25 print(f"Age: {age}") # Age: 25

SyntaxError — Python can't even read the code

A SyntaxError means the code itself is written incorrectly, so Python can't run it at all. Common causes: a missing colon, an unclosed quote or bracket, or wrong indentation:

Python
if age > 18 # SyntaxError: missing colon print("Adult")

The fix here is the missing : after the condition:

Python
if age > 18: print("Adult") ```…