AlgoBazaAlgoBaza
Python for kids ยท Module 2

Lesson 29

Mistakes in ifs

After this lesson: You'll find and fix mistakes in ifs - including the ones that give no message at all.

Video

?

The video for this lesson is coming soon.

Explanation

Detective Bajtek is back! ๐Ÿ•ต๏ธ It's easy to make mistakes in ifs. Some of them Python reports straight away (a red message). But there are also sneaky ones - the program runs, it just gives the wrong answer. Today we'll learn to catch both kinds.

Three clues in an if ๐ŸŽฌ

Click Next - three mistakes that Python reports straight away.

?Bajtek

Code

n = 5
if n > 0
print("TAK")

๐Ÿ–ฅ๏ธ Screen

SyntaxError: expected ':'
๐Ÿ” the : is missing
1

Clue 1: no colon

Kuba writes if n > 0 - with no colon. Python: SyntaxError: expected ':'. The message says exactly what's missing!

Step 1 / 4

Clue 1 live

The colon is missing - Python runs nothing.

Code
1n = 5
2if n > 0
3 print("TAK")
Screen
File "<lesson>", line 2
    if n > 0
            ^
SyntaxError: expected ':'
1/1 ยท the program stopped with an error

Clue 2 live

One = in the condition - read Python's hint.

Code
1n = 5
2if n = 5:
3 print("TAK")
Screen
File "<lesson>", line 2
    if n = 5:
       ^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1/1 ยท the program stopped with an error

Clue 3 live

The line under the if isn't indented.

Code
1n = 5
2if n > 0:
3print("TAK")
Screen
File "<lesson>", line 3
    print("TAK")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 2
1/1 ยท the program stopped with an error

More clues

CodeMessageWhat to fix
else with no colonSyntaxError: expected ':'add the :
else: indented differently from ifSyntaxError: invalid syntaxline else up with the if
elif with no if above itSyntaxError: invalid syntaxan elif must come after an if
If n > 0:SyntaxError: invalid syntaxif in small letters
if age > 0:, but the variable is aegNameError: name 'age' is not definedthe same name everywhere

A mistake with no message ๐ŸŽฌ

Click Next - the sneakiest kind of mistake.

?Bajtek

Code

if height > 120:
print("TAK")
else:
print("NIE")

๐Ÿ–ฅ๏ธ Screen

TAK
NIE
1

The program runs... but wrong

Ola writes a program for a ride from 120 cm: if height > 120:. No error! Ola tests 150 - TAK. And 100 - NIE. Does it all work?

Step 1 / 3

Find the bug in the ride ๐ŸŽข

This program has a sneaky bug. Type a height ๐Ÿ“ - check 120, 119 and 121. Which result is wrong?

?

Click the arrow - we'll put every comparison on the scale.

Code
1height_str = input()
2height = int(height_str)
3if height > 120:
4 print("TAK")
5else:
6 print("NIE")
Screen
โ–
1/2 ยท line 1 runs next

Sneaky bugs - the detective's list

These bugs give no message - the program runs, but answers wrong:

Suspicious codeWhat's wrongThe fix
if height > 120: for "from 120"the limit doesn't count>=
if day == 6 or 7:or 7 is always Trueday == 6 or day == 7
if ans == 5: after ans = input()text is never equal to a numberint(ans) == 5
if p >= 50: before elif p >= 90:the first True wins too earlythe highest limit first
if n >= 1 or n <= 10: for "from 1 to 10"almost any number fitsand instead of or

The or 7 trap

Day 3 is Wednesday - and the program says Weekend! Look at the switches: 7 is always on.

๐Ÿ”‹ โ”€ โป โ”€ ๐Ÿ’ก

Click the arrow - you'll see the condition as a circuit with switches and a bulb.

Code
1day = 3
2if day == 6 or 7:
3 print("Weekend!")
4else:
5 print("School")
Screen
โ–
1/4 ยท line 1 runs next

The fixed or

Now there's a whole question on both sides of or - for Wednesday both switches are off.

๐Ÿ”‹ โ”€ โป โ”€ ๐Ÿ’ก

Click the arrow - you'll see the condition as a circuit with switches and a bulb.

Code
1day = 3
2if day == 6 or day == 7:
3 print("Weekend!")
4else:
5 print("School")
Screen
โ–
1/4 ยท line 1 runs next

The detective's trick: print along the way

When you don't know which road the program took, add print("I'm in the if") or print(condition) for a moment - for example print(height > 120). You'll see what Python really checks. Then remove those lines!

?

Which line has a mistake?

n = 4
if n == 4
print("four")
?

What's wrong with this condition: if age = 18:

?

The program should print TAK for numbers from 1 to 10. Which condition is right?

?

Which numbers do you type to test the program "from 50 points - passed"?

Remember

  • Mistakes with a message: no :, = instead of ==, no indentation, a misplaced else.
  • Read the message: line number + error name + description (Python often gives a hint!).
  • Sneaky bugs give a wrong answer with no message: a wrong limit, or 7, text instead of a number, the elif order.
  • Test the limits (119, 120, 121) and every road of the program.
  • A trick: print(condition) shows what Python really checks.

Now the detective's tasks: each one has a broken program. Find the mistake and submit the fixed version!

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.