Comments and reading error messages
Leave notes for humans with comments, then learn to read Python's error messages calmly, bottom line first, and fix the most common beginner mistakes.

In this lesson you will
- Write comments with
- Use a comment to switch a line off temporarily
- Read an error message, starting from the last line
- Recognise SyntaxError, NameError, TypeError, ValueError and ZeroDivisionError and know what usually causes each
Two skills in this lesson, and both are about communication. Comments are how you talk to the humans who read your code. Error messages are how Python talks to you. Getting good at both will make you a calmer, faster programmer.
Comments: notes for humans
Anything after a # on a line is a comment. Python ignores it completely. Comments are
there for people: other programmers, and you in a few weeks’ time.
# Work out the cost of a school trip
students = 28
cost_each = 12.5 # dollars, including the bus
print(students * cost_each)A comment can sit on its own line or at the end of a line of code. Good comments explain why, or what a number means, not what is obvious from the code itself:
lives = 3 # bad comment: set lives to 3
lives = 3 # good comment: the original arcade game also started with 3
Switching a line off
Comments have a handy second use. Put a # in front of a line of code and Python skips it. This
is called commenting out a line, and it’s a quick way to test what happens without it:
print("one")
# print("two")
print("three")Remove the # and run again to bring line 2 back.
Errors are messages, not failures
When Python can’t carry on, it stops and prints an error message, also called a traceback. It can look alarming the first time, with all that red text, but every error message answers three questions: what went wrong, where, and often a hint about why.
Here’s a short program with a typo on line 4. Step through it and watch what happens when Python reaches the bad line.
Here is a short program with a mistake hidden in it. Watch what happens as Python runs it.
The program got as far as line 4 and stopped. Line 5 never ran, which is why nothing was printed. A full traceback for that program looks like this:
Traceback (most recent call last):
File "main.py", line 4, in <module>
total = price * cuont
^^^^^
NameError: name 'cuont' is not defined. Did you mean: 'count'?
Read it from the bottom up
The most useful line is the last one. Start there:
- The last line names the kind of error (
NameError) and describes it in words: there’s no such name ascuont. Recent versions of Python even guess what you meant. - Just above is the line of code that failed, often with
^^^^marks pointing at the exact spot. - Above that is where it happened:
line 4.
So: go to line 4, look at cuont, notice it should be count, fix it. Done. You’ll rarely need
the rest of the traceback until your programs get bigger.
The five errors you’ll meet most
| Error | What it means | Usual cause |
|---|---|---|
SyntaxError | Python can’t even understand the line | a missing quote, bracket or colon |
NameError | a name that doesn’t exist | a typo, or a variable used before it was created |
TypeError | the right operation on the wrong type | "3" + 4, mixing text and numbers |
ValueError | the right type, but an impossible value | int("seven") |
ZeroDivisionError | dividing by zero | a divisor that ended up as 0 |
A SyntaxError is special: Python checks the whole cell before running any of it, so a
syntax error means nothing runs, not even the lines above the mistake. With the other
errors, the lines before the problem run normally, just like in the stepper above.
Practise reading errors
The cell below is fine. Break it on purpose, one way at a time, run it, read the last line of the error, then undo your change:
- Delete the closing
)on the last line. (SyntaxError) - Change
widthtowidhton the last line. (NameError) - Put quotes around the
3, so it says"3". (TypeError) - Change
height = 4toheight = 0, and the*on the last line to/. (ZeroDivisionError)
width = 3
height = 4
print("Area:", width * height)Doing this deliberately, while you already know the cause, is the best way to learn what each message looks like. Next time you see one by accident, you’ll recognise it.
Mistakes without error messages
The sneakiest bugs don’t produce an error at all. Remember the average that forgot its brackets in the arithmetic lesson? Python was happy; the answer was simply wrong. The cure is a habit: predict what your code should print before you run it, then compare. When the two differ, you’ve found something worth investigating. You’ll practise this properly in the lesson on tracing and debugging.
Your turn
This exercise starts with broken code. Run it, read the errors, and fix them one at a time.
Exercise · Python
Fix the bugs
Solved ✓This program should print a sentence about a garden and store the garden’s area in area.
It has three bugs. Run it, read the last line of each error message, fix that bug, and
run again until it works:
areashould be the length times the width (12 × 5 = 60),messageshould be"The garden is 60 square metres".
length = 12
width = 5
area = lenght * width
message = "The garden is " + area + " square metres
print(message)
Check yourself
Self-check · 4 questions