Errors are your friends
🌍 In real life. When a teacher marks your homework, a good mark doesn't just say "wrong". It circles the exact spot and writes what's wrong next to it: "spelling", "missing full stop". That makes it quick to fix.
Python's error messages are that kind of marking. Every programmer, even an expert, sees them every day. An error isn't a punishment; it's a clue that points at the problem.
The idea in one sentence: an error message tells you where Python got stuck (a line number), what kind of problem it is (the error's name) and why, in a short description.
Its words:
- error: Python saying "I can't do this line"
- traceback: the full error message, ending with the most important line
- SyntaxError: the code breaks Python's writing rules
- NameError: Python doesn't know a name you used
- IndentationError: spaces in the wrong place at the start of a line
Step by step, to fix any error:
- Read the last line of the message first: the error's name and why.
- Go to the line number it gives.
- Look closely for a typo, a missing quote or bracket, or extra spaces.
- Fix one thing, then run again.
How Python does it: two moments.
- Before running anything, Python reads the whole program to check its writing rules. A SyntaxError or IndentationError is found here, so not even line 1 runs.
- While running, line by line. A NameError is found here, when Python reaches that line. The lines before it have already run, and Python stops at the error, so the lines after it never run.
That's why a typo like prnt lets the first line print, while a missing quote prints nothing at all.
print("This runs")
prnt("Oops, a typo")
print("This never runs")print("This line is fine")
print("Missing a closing quote)print("Line one")
print("Line two")Mistakes and edge cases.
- Reading only the top of the message. The last line holds the error's name and reason; start there.
- Missing the hint. Some messages end with a suggested fix, like
Did you mean print(...)?forprint "Hi". Read to the very end. - Fixing many things at once. Fix the first error, run again, and see what's left. One fix often makes the next message disappear.
- Extra spaces at the start of a line.
IndentationError: unexpected indent. For now, start every line at the very left edge. - The line number is close, not exact. For a missing bracket, the real mistake can be on the line just above the one named.
🕵️ Errors are normal. The skill isn't avoiding them; it's reading them calmly.
Quiz
What happens to the lines after an error?
- They still run
- They don't run: Python stops at the error
- They run twice
Which line causes the error?
print("Hello") print("World" print("!")Print("Hi") gives a NameError. Why?
- Python cares about capital letters: it must be print in small letters
- Hi doesn't need quotes
- The brackets are the wrong shape
What is printed before the error stops the program?
print("start") prnt("middle") print("end")What does this program print? print("one") is on line 1, and line 2 is print("two) with a missing quote.
- one, then the error
- Nothing, just the error
- one and two
Coding tasks
This code has a SyntaxError. Fix it so it shows
Hello.This code has a NameError. Fix it so it shows
Python.This code has an IndentationError. Fix it so it shows both lines.