Lesson 13
Errors - reading the messages
After this lesson: You'll read an error message, find the line with the mistake and fix it.
Video
The video for this lesson is coming soon.
Explanation
Every programmer makes mistakes - even me! What matters is finding them. Python always tells you what's wrong.
How to read an error message
When a program has a mistake, Python stops and prints a message. It looks e.g. like this:
File "main.py", line 3
print(resultt)
NameError: name 'resultt' is not defined
Two things matter most:
- the line number (
line 3) - look for the mistake there, - the error name at the very bottom (
NameError) and its description - here: "there's no name resultt".
Bajtek the bug detective ๐ต๏ธ
Click Next to go step by step.
Code
๐ฅ๏ธ Screen
The program stopped!
Kuba wrote a one-line program. It should have said hello, but instead of Hi! some strange red text appeared. Kuba is worried - what broke?
Where does this error come from? ๐
Click the arrow and watch the cards with the names Python knows. Line 1 makes the box result, and in line 2 Python looks for the name resultt - will it find it?
Click the arrow. For every name Python checks its list to see if it knows it.
1result = 102print(resultt)
โThe most common errors
| Error | What it means | Example |
|---|---|---|
SyntaxError | badly written code (missing bracket or quote) | print("Hi" |
NameError | no such name (a typo, Print instead of print) | Print("Hi") |
TypeError | text and a number can't be joined | "2" + 3 |
ZeroDivisionError | division by zero | 5 / 0 |
IndentationError | wrong indentation (a space at the start of a line) | print(1) |
You'll see each of them in action in a moment.
1. SyntaxError - a missing bracket
The closing bracket ) is missing. Python reads the whole program before running it - and already then it sees the code is written wrong. That's why not even the first line runs! Fix: add the missing bracket.
1print("Start")2print("Hej"
File "<lesson>", line 2
print("Hej"
^
SyntaxError: '(' was never closed2. NameError - a typo in a name
The first line works. The second has Print with a capital letter - Python doesn't know that name and stops. Fix: print in small letters.
Click the arrow. For every name Python checks its list to see if it knows it.
1print("Start")2Print("Hej")
โNameError - a typo in a variable name
The box is called result, but print has resultt - with two t's. Python looks for such a box on its list and doesn't find it. Fix: the same name as when the box was made.
Click the arrow. For every name Python checks its list to see if it knows it.
1result = 5 + 32print(resultt)
โ3. TypeError - text plus a number
The first line works, and on the second the program stops. Why? Bajtek explains below. ๐
On the left the text "2" (in quotes!), and on the right the number 3. What will the plus do?
1print("Start")2print("2" + 3)
โWhy can't you add text and a number? Step by step:
- The + sign can do two different things.
- When there are numbers on both sides - it adds:
2 + 3is 5. - When there's text on both sides - it glues:
"2" + "3"is "23". - But
"2" + 3is text and a number at once. Python doesn't know: add and give 5, or glue and give "23"? ๐คท Instead of guessing, it stops and says TypeError.
Three cases:
| What is on both sides of + | What Python does | Result |
|---|---|---|
2 + 3 - two numbers ๐ข๐ข | adds | 5 โ
|
"2" + "3" - two pieces of text ๐ค๐ค | glues | "23" โ
|
"2" + 3 - text and a number ๐ค๐ข | doesn't know what to do ๐คท | TypeError โ |
Two fixes, two different results
Two numbers - Python adds: 5. Two pieces of text - Python glues: 23. That's why it has to know which one you mean.
The text is in the variable now. Every print shows a different trick with text.
1print(2 + 3)2print("2" + "3")
โ4. ZeroDivisionError - division by zero
You can't divide by zero - not on paper and not in Python. Fix: divide by another number.
Click the arrow. In a moment Python tries to divide 5 by 0.
1print("Start")2print(5 / 0)
โ5. IndentationError - a space at the start
The second line starts with a space. A line can't start with a space (you'll learn about indents later). Like with a SyntaxError, Python sees it right away - nothing runs. Fix: remove the space.
Line 2 starts with a space - it sticks out from the left edge! Python sees it straight away, so it runs nothing, not even the first line. The fix: remove the space.
1print("Start")2print(1)
File "<lesson>", line 2
print(1)
IndentationError: unexpected indentA capital letter in a name
Watch the list of names: after the first line it has points (small letter), but print looks for the name Points (capital). There is no such name!
Click the arrow. For every name Python checks its list to see if it knows it.
1points = 102print(Points)
โWhy a NameError when the box is there? Step by step:
- For Python a capital and a small letter are two different letters - just like
aandb. - The box is called
points- with a small letter. print(Points)looks for a boxPoints- with a capital letter. That's a completely different name!- There's no such box, so Python says NameError - "I don't know that name".
Fix: write the name exactly the same, letter by letter: print(points).
Fixed: the same name โ
Now print looks for the box points - exactly the one we created.
Click the arrow and watch the values move between the boxes.
1points = 102print(points)
โA number plus text
The box apples holds the number 5, and we want to stick text next to it. Watch what happens on the second line.
Click the arrow - let's see what the plus does with text and a number.
1apples = 52print(apples + " apples")
โWhy a TypeError? Step by step:
- The box
applesholds the number 5 ๐ข. " apples"is in quotes - it's text ๐ค.- A number + text is the same trouble as
"2" + 3: Python doesn't know whether to add or glue - so it stops.
Fix: use a comma instead of the plus: print(apples, "apples"). A comma doesn't add or glue - it just prints both things next to each other and puts the space in by itself.
Fixed: a comma instead of a plus โ
With a comma print prints the number from the box, a space and the text.
Click the arrow - let's see what the plus does with text and a number.
1apples = 52print(apples, "apples")
โErrors that stop the program right away
SyntaxError and IndentationError are special: Python notices them before running the program, so no line runs - not even the correct ones before the mistake. Examples:
print("Hi"- the closing bracket is missing,print("Hi)- the closing quote is missing,print("Hi")- a space at the start of the line.
Python - a careful reader ๐ฌ
Click Next to go step by step.
Code
๐ฅ๏ธ Screen
First Start, then Hi
Kuba writes a two-line program and clicks Run. He thinks: the first line is fine, so at least Start will be printed. But the screen stays empty!
A missing bracket )
Watch Python check every line in turn before it runs anything.
๐ Python reads the whole program before running it
In line 2 a bracket ( opens but never closes - like a door nobody shut. SyntaxError! No line runs, not even print("Start").
1print("Start")2print("Hi"
File "<lesson>", line 2
print("Hi"
^
SyntaxError: '(' was never closedA missing quote "
A quote opens but doesn't close - Python doesn't know where the text ends.
๐ Python reads the whole program before running it
In line 2 a quote opens but never closes - Python doesn't know where the text ends. SyntaxError! No line runs, not even print("Start").
1print("Start")2print("Hi)
File "<lesson>", line 2
print("Hi)
^
SyntaxError: unterminated string literal (detected at line 2)A straight line ๐ฌ
Click Next and see why one space breaks the whole program.
Code
๐ฅ๏ธ Screen
Lines in a row
The lines of a program are like children lining up ๐ง๐ง: each one stands straight at the left edge, one under the other. Then Python reads them easily and the program works.
A space at the start of a line
The second line is pushed one space to the right - that's an IndentationError.
๐ Python reads the whole program before running it
Line 2 starts with a space - Python doesn't know what to do with it. IndentationError! Python finds this before running anything, so no line runs.
1print("Start")2print("Hi")
File "<lesson>", line 2
print("Hi")
IndentationError: unexpected indentHow to find a mistake - 4 steps
- Read the error name at the bottom of the message.
- Find the line with the given number.
- Compare it with the correct form: are both quotes there? Both brackets? Is the name the same as when the variable was created?
- Fix it and run again. Sometimes another error shows up after the fix - that's normal, fix them one at a time.
Which error does Python report for print("Hi"?
Which error does this program report?
points = 5print(Points)
Which line has the mistake?
print("Ala")print("Ola"print("Kuba")
Mistakes Python won't notice
Sometimes a program runs but prints the wrong result - e.g. it calculates the perimeter instead of the area, or adds instead of multiplying. Python doesn't know what you wanted - it does exactly what you wrote. Then the judge says Wrong answer. Compare your result with the example in the task!
It runs, but it's wrong
The program should print the area of a square with side 5 (that's 25), but it prints the perimeter. Python reports no error!
The task: print the area of a square with side ?. Click the arrow and see what the program works out.
1side = 52print(4 * side)
โMistakes are nothing scary - every programmer makes loads of them! What matters is finding them. Let's play detective ๐
Detective: a typo in a name
The variable is called result, but print has reslt. Python doesn't know such a box.
Click the arrow. For every name Python checks its list to see if it knows it.
1result = 7 * 62print(reslt)
โDetective: text plus a number
Text and a number can't be added. The message says TypeError.
Click the arrow - let's see what the plus does with text and a number.
1age = 102print("I am " + age)
โDetective: division by zero
This can't be done even on paper - Python gives up too.
Click the arrow. In a moment Python tries to divide 10 by 0.
1print("Starting")2print(10 // 0)3print("You won't see this")
โDetective: a wrong perimeter
The program runs, but for a 5 x 3 rectangle the perimeter is 16, not 13. Where's the mistake?
1a = 52b = 33perimeter = 2 * a + b4print(perimeter)
โFixed: brackets
2 * (a + b) - twice the sum of the sides.
1a = 52b = 33perimeter = 2 * (a + b)4print(perimeter)
โPrint what's in the boxes
A detective's trick: print the variables along the way and see where the result goes wrong.
The detective checks what's in the boxes one by one. Click the arrow - every print is a new clue. ๐ต๏ธ
1candies = 202kids = 63each = candies / kids4print("each =", each)5each = candies // kids6print("each =", each)
โThe program stops at the error
Did you notice? The first line was printed, but the last one wasn't. Python runs the program until it hits an error - then it stops and shows a message. Everything below the error doesn't run.
That's why the line number in the message is so important: it tells you where the program stopped. ๐
Which error appears in this program?
print("Ala ma kota)
A program has 5 lines and the error is on line 3. How many lines run?
Remember
- Read the message from the bottom: the error name and the line number.
- SyntaxError - a missing bracket or quote.
- NameError - a typo in a name or the variable doesn't exist yet.
- A wrong result with no error - compare with the example and check the formula.
In the tasks you'll get broken programs. Your mission: find the mistake and fix it. Good luck, detective!
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.