AlgoBazaAlgoBaza
Python for kids ยท Module 1

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.

?Bajtek
Did I break the computer?๐Ÿ˜Ÿ

Code

Print("Hi!")

๐Ÿ–ฅ๏ธ Screen

File "main.py", line 1
Print("Hi!")
NameError: name 'Print' is not defined
โŒ the program stopped
1

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?

Step 1 / 7

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?

๐Ÿ”Ž Looking for:?
๐Ÿ“– Names Python knows
printinputint

Click the arrow. For every name Python checks its list to see if it knows it.

Code
1result = 10
2print(resultt)
Screen
โ–
1/3 ยท line 1 runs next

The most common errors

ErrorWhat it meansExample
SyntaxErrorbadly written code (missing bracket or quote)print("Hi"
NameErrorno such name (a typo, Print instead of print)Print("Hi")
TypeErrortext and a number can't be joined"2" + 3
ZeroDivisionErrordivision by zero5 / 0
IndentationErrorwrong 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.

Code
1print("Start")
2print("Hej"
Screen
File "<lesson>", line 2
    print("Hej"
         ^
SyntaxError: '(' was never closed
1/1 ยท the program stopped with an error

2. 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.

๐Ÿ”Ž Looking for:?
๐Ÿ“– Names Python knows
printinputintstrlen

Click the arrow. For every name Python checks its list to see if it knows it.

Code
1print("Start")
2Print("Hej")
Screen
โ–
1/3 ยท line 1 runs next

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.

๐Ÿ”Ž Looking for:?
๐Ÿ“– Names Python knows
printinputint

Click the arrow. For every name Python checks its list to see if it knows it.

Code
1result = 5 + 3
2print(resultt)
Screen
โ–
1/3 ยท line 1 runs next

3. TypeError - text plus a number

The first line works, and on the second the program stops. Why? Bajtek explains below. ๐Ÿ‘‡

"2"๐Ÿ”ค text
+
3๐Ÿ”ข number
?

On the left the text "2" (in quotes!), and on the right the number 3. What will the plus do?

Code
1print("Start")
2print("2" + 3)
Screen
โ–
1/3 ยท line 1 runs next
?

Why can't you add text and a number? Step by step:

  1. The + sign can do two different things.
  2. When there are numbers on both sides - it adds: 2 + 3 is 5.
  3. When there's text on both sides - it glues: "2" + "3" is "23".
  4. But "2" + 3 is 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 doesResult
2 + 3 - two numbers ๐Ÿ”ข๐Ÿ”ขadds5 โœ…
"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.

Code
1print(2 + 3)
2print("2" + "3")
Screen
โ–
1/3 ยท line 1 runs next

4. ZeroDivisionError - division by zero

You can't divide by zero - not on paper and not in Python. Fix: divide by another number.

5 to share
๐Ÿช๐Ÿช๐Ÿช๐Ÿช๐Ÿช
๐Ÿช‘ ๐Ÿช‘ ๐Ÿช‘0 children - nobody to give them to!

Click the arrow. In a moment Python tries to divide 5 by 0.

Code
1print("Start")
2print(5 / 0)
Screen
โ–
1/3 ยท line 1 runs next

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.

โ–left edge
1print("Start")โœ…
2โฃprint(1)โŒ
IndentationError โŒ
๐Ÿ’ก Without the space
1print("Start")โœ…
2print(1)โœ…

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.

Code
1print("Start")
2 print(1)
Screen
File "<lesson>", line 2
    print(1)
IndentationError: unexpected indent
1/1 ยท the program stopped with an error

A 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!

๐Ÿ”Ž Looking for:?
๐Ÿ“– Names Python knows
printinputint

Click the arrow. For every name Python checks its list to see if it knows it.

Code
1points = 10
2print(Points)
Screen
โ–
1/3 ยท line 1 runs next
?

Why a NameError when the box is there? Step by step:

  1. For Python a capital and a small letter are two different letters - just like a and b.
  2. The box is called points - with a small letter.
  3. print(Points) looks for a box Points - with a capital letter. That's a completely different name!
  4. 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.

not yet
๐Ÿ“ฆ points

Click the arrow and watch the values move between the boxes.

Code
1points = 10
2print(points)
Screen
โ–
1/3 ยท line 1 runs next

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.

?๐Ÿ”ค text
+
๐Ÿ”ข number
?

Click the arrow - let's see what the plus does with text and a number.

Code
1apples = 5
2print(apples + " apples")
Screen
โ–
1/3 ยท line 1 runs next
?

Why a TypeError? Step by step:

  1. The box apples holds the number 5 ๐Ÿ”ข.
  2. " apples" is in quotes - it's text ๐Ÿ”ค.
  3. 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.

?๐Ÿ”ค text
+
๐Ÿ”ข number
?

Click the arrow - let's see what the plus does with text and a number.

Code
1apples = 5
2print(apples, "apples")
Screen
โ–
1/3 ยท line 1 runs next

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.

?Bajtek
Not even Start was printed?๐Ÿ˜•

Code

print("Start")
print("Hi"

๐Ÿ–ฅ๏ธ Screen

SyntaxError: '(' was never closed
โŒ nothing ran
1

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!

Step 1 / 5

A missing bracket )

Watch Python check every line in turn before it runs anything.

๐Ÿ” Python reads the whole program before running it

1๐Ÿ”print("Start")โœ… fine
2๐Ÿ”print("Hi"?โŒ missing )
๐Ÿ’ฅ SyntaxError๐Ÿšซ none of the 2 lines runs

In line 2 a bracket ( opens but never closes - like a door nobody shut. SyntaxError! No line runs, not even print("Start").

Code
1print("Start")
2print("Hi"
Screen
File "<lesson>", line 2
    print("Hi"
         ^
SyntaxError: '(' was never closed
1/1 ยท the program stopped with an error

A 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

1๐Ÿ”print("Start")โœ… fine
2๐Ÿ”print("Hi)?โŒ missing "
๐Ÿ’ฅ SyntaxError๐Ÿšซ none of the 2 lines runs

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").

Code
1print("Start")
2print("Hi)
Screen
File "<lesson>", line 2
    print("Hi)
          ^
SyntaxError: unterminated string literal (detected at line 2)
1/1 ยท the program stopped with an error

A straight line ๐ŸŽฌ

Click Next and see why one space breaks the whole program.

Bajtek
๐Ÿง’line 1๐Ÿง’line 2๐Ÿ“straight at the left edge

Code

print("Start")
print("Hi")

๐Ÿ–ฅ๏ธ Screen

Start
Hi
โœ… a straight line
1

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.

Step 1 / 5

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

1๐Ÿ”print("Start")โœ… fine
2๐Ÿ”print("Hi")โŒ a space at the start
๐Ÿ’ฅ IndentationError๐Ÿšซ none of the 2 lines runs

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.

Code
1print("Start")
2 print("Hi")
Screen
File "<lesson>", line 2
    print("Hi")
IndentationError: unexpected indent
1/1 ยท the program stopped with an error

How to find a mistake - 4 steps

  1. Read the error name at the bottom of the message.
  2. Find the line with the given number.
  3. Compare it with the correct form: are both quotes there? Both brackets? Is the name the same as when the variable was created?
  4. 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 = 5
print(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.

Code
1side = 5
2print(4 * side)
Screen
โ–
1/3 ยท line 1 runs next

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.

๐Ÿ”Ž Looking for:?
๐Ÿ“– Names Python knows
printinputintresult

Click the arrow. For every name Python checks its list to see if it knows it.

Code
1result = 7 * 6
2print(reslt)
Screen
โ–
1/3 ยท line 1 runs next

Detective: text plus a number

Text and a number can't be added. The message says TypeError.

?๐Ÿ”ค text
+
๐Ÿ”ข number
?

Click the arrow - let's see what the plus does with text and a number.

Code
1age = 10
2print("I am " + age)
Screen
โ–
1/3 ยท line 1 runs next

Detective: division by zero

This can't be done even on paper - Python gives up too.

10 to share
๐Ÿช๐Ÿช๐Ÿช๐Ÿช๐Ÿช๐Ÿช๐Ÿช๐Ÿช๐Ÿช๐Ÿช
๐Ÿช‘ ๐Ÿช‘ ๐Ÿช‘0 children - nobody to give them to!
๐Ÿ‘‰print("Starting")
ยทprint(10 // 0)
ยทprint("You won't see this")

Click the arrow. In a moment Python tries to divide 10 by 0.

Code
1print("Starting")
2print(10 // 0)
3print("You won't see this")
Screen
โ–
1/3 ยท line 1 runs next

Detective: a wrong perimeter

The program runs, but for a 5 x 3 rectangle the perimeter is 16, not 13. Where's the mistake?

๐Ÿ“ โ€ฆ
Code
1a = 5
2b = 3
3perimeter = 2 * a + b
4print(perimeter)
Screen
โ–
1/5 ยท line 1 runs next

Fixed: brackets

2 * (a + b) - twice the sum of the sides.

๐Ÿ“ โ€ฆ
Code
1a = 5
2b = 3
3perimeter = 2 * (a + b)
4print(perimeter)
Screen
โ–
1/5 ยท line 1 runs next

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. ๐Ÿ•ต๏ธ

Code
1candies = 20
2kids = 6
3each = candies / kids
4print("each =", each)
5each = candies // kids
6print("each =", each)
Screen
โ–
1/7 ยท line 1 runs next

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.