Finding and Fixing Mistakes in Python
How to read error messages and fix common beginner bugs
- stay calm when you see a red error message,
- read a Python traceback to find the line number and the kind of error,
- recognise common beginner errors like
NameError,TypeError, andSyntaxError, - use
print()to peek inside your program and find the bug.
1. A bug is not a disaster
A mistake in code is called a bug. Fixing bugs is called debugging. Bugs are completely normal — they are part of coding, like erasing and re-writing is part of drawing.
Analogy: Think of Python as a very careful friend reading your recipe out loud. If a step does not make sense, your friend stops and points at the exact line: "Here — I do not understand this part." Python does the same thing with an error message. It is trying to help you, not scold you.
2. How to read a traceback
When Python hits a problem it cannot continue past, it stops and prints a traceback. Look at this tiny program that has a typo:
message = "Hello"
print(mesage)
When you run it, Python prints:
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(mesage)
NameError: name 'mesage' is not defined
Do not panic — read it from the bottom up. The most useful parts are:
- The last line tells you the type of error and a short message. Here it is
NameError: name 'mesage' is not defined. - The
line 2tells you exactly where to look. The bug is on line 2.
Now the fix is easy: we wrote mesage but the box is called message. Fix the spelling:
message = "Hello"
print(message)
Expected output:
Hello