Errors and Debugging¶
Try This First: Before reading, try running
print(1/0)in Python. What happens? Read the error message -- it tells you exactly what went wrong.
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize |
|---|---|---|---|---|---|
| You are here | Projects | Videos | Quiz | Flashcards | Diagrams |
Errors are not failures. They are Python telling you exactly what went wrong and where. Learning to read error messages is one of the most valuable skills in programming.
Visualize It¶
See what happens when Python hits an error — watch the execution stop: Open in Python Tutor
Anatomy of an error message¶
Traceback (most recent call last):
File "exercise.py", line 5, in <module>
print(score)
NameError: name 'score' is not defined
Read it bottom-up:
1. NameError: name 'score' is not defined — what went wrong
2. File "exercise.py", line 5 — where it happened
3. print(score) — the exact line that caused it
Common error types¶
| Error | Meaning | Example |
|---|---|---|
SyntaxError |
Python cannot understand your code | Missing colon, unmatched quotes |
NameError |
You used a name that does not exist | Typo in variable name |
TypeError |
Wrong type for the operation | Adding a string and a number |
IndexError |
List index out of range | my_list[99] when list has 5 items |
KeyError |
Dict key does not exist | my_dict["missing_key"] |
FileNotFoundError |
File does not exist at that path | Wrong filename or directory |
ValueError |
Right type but wrong value | int("hello") |
IndentationError |
Indentation is inconsistent | Mixed tabs and spaces |
Debugging strategy¶
When something goes wrong:
- Read the error message. It usually tells you exactly what happened.
- Look at the line number. Go to that line in your file.
- Check spelling. Typos in variable names cause
NameError. - Print things. Add
print(variable_name)before the error line to see what values actually are. - Simplify. Remove code until you find the smallest version that still breaks.
The print() debugging method¶
The simplest debugging technique: print values to see what is happening.
data = load_file("input.txt")
print("data is:", data) # What did we actually load?
print("type is:", type(data)) # Is it a list? a string? None?
print("length is:", len(data)) # How many items?
for item in data:
print("processing:", item) # See each item as it is processed
Common mistakes and fixes¶
SyntaxError — missing colon:
TypeError — mixing strings and numbers:
age = 30
print("I am " + age) # Error! Cannot add string + int
print("I am " + str(age)) # Fixed with str()
print(f"I am {age}") # Better — use f-string
IndentationError:
if True:
print("hello") # Error! Must be indented
if True:
print("hello") # Fixed — 4 spaces of indentation
Top 5 beginner mistakes¶
These five mistakes account for the vast majority of errors new programmers hit. Learning to spot them quickly will save you hours of frustration.
1. IndentationError — your code is not lined up¶
Python uses indentation (spaces at the start of a line) to know which code belongs inside an if, for, or function. If the spacing is off, Python refuses to run your code at all.
The error message:
Broken code:
if temperature > 100:
print("Too hot!") # Not indented — Python does not know this belongs to the if
The fix:
Why it happens: Unlike most languages, Python does not use curly braces {} to group code. It uses indentation instead. Every line inside an if, for, while, or def must be indented by the same amount (use 4 spaces — not tabs).
2. NameError — Python does not recognize a name¶
This usually means you misspelled a variable name, or you tried to use a variable before creating it.
The error message:
Broken code:
The fix:
Why it happens: Python is case-sensitive and spelling-sensitive. message, Message, and mesage are three completely different names. Python only knows about names you have already created on a previous line.
3. SyntaxError — missing colon after if/for/while/def¶
Every if, elif, else, for, while, and def line must end with a colon :. Forget it, and Python cannot understand your code.
The error message:
Broken code:
The fix:
Why it happens: The colon tells Python "the next indented block belongs to this statement." Without it, Python sees an incomplete line and does not know what you meant. This is easy to forget because colons do not exist in everyday writing the same way.
4. SyntaxError — mismatched parentheses or brackets¶
Every opening (, [, or { needs a matching closing ), ], or }. Miss one and Python gets confused, sometimes pointing to a line that looks perfectly fine.
The error message:
Broken code:
The fix:
Why it happens: Python reads your code from top to bottom. When it sees [, it expects everything until ] to be part of the list. Without the closing bracket, it keeps reading into the next line and gets confused. The error message often points to a different line than where the real problem is, so check the lines above where Python complains.
Tip: Most code editors highlight matching brackets. If you place your cursor next to a ( and its partner does not light up, that is your clue.
5. TypeError — forgetting to convert types¶
Python will not automatically turn a number into a string or vice versa. If you try to combine them, you get a TypeError.
The error message:
Broken code:
The fix (three ways):
age = 25
# Option 1 — convert with str()
print("I am " + str(age) + " years old")
# Option 2 — f-string (recommended, cleanest)
print(f"I am {age} years old")
# Option 3 — comma in print (adds spaces automatically)
print("I am", age, "years old")
Why it happens: Python keeps types strict on purpose. A number 25 and the text "25" look the same to us, but Python stores them differently. Requiring you to convert explicitly prevents subtle bugs — for example, "2" + "5" gives "25" (text joined together), while 2 + 5 gives 7 (math). Python wants you to be clear about which one you mean.
Practice¶
- Level 00 / 08 Making Decisions
- Level 00 / 14 Reading Files
- Level 00 / 15 Putting It Together
- Level 0 / 07 First File Reader
- Level 1 / 01 Input Validator Lab
- Level 1 / 08 Path Exists Checker
- Level 1 / 11 Command Dispatcher
- Level 2 / 04 Error Safe Divider
- Level 2 / 11 Retry Loop Practice
Quick check: Take the quiz
Review: Flashcard decks Practice reps: Coding challenges
| ← Prev | Home | Next → |
|---|---|---|