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.
Code
๐ฅ๏ธ Screen
Clue 1: no colon
Kuba writes if n > 0 - with no colon. Python: SyntaxError: expected ':'. The message says exactly what's missing!
Clue 1 live
The colon is missing - Python runs nothing.
1n = 52if n > 03print("TAK")
File "<lesson>", line 2
if n > 0
^
SyntaxError: expected ':'Clue 2 live
One = in the condition - read Python's hint.
1n = 52if n = 5:3print("TAK")
File "<lesson>", line 2
if n = 5:
^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?Clue 3 live
The line under the if isn't indented.
1n = 52if n > 0:3print("TAK")
File "<lesson>", line 3
print("TAK")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 2More clues
| Code | Message | What to fix |
|---|---|---|
else with no colon | SyntaxError: expected ':' | add the : |
else: indented differently from if | SyntaxError: invalid syntax | line else up with the if |
elif with no if above it | SyntaxError: invalid syntax | an elif must come after an if |
If n > 0: | SyntaxError: invalid syntax | if in small letters |
if age > 0:, but the variable is aeg | NameError: name 'age' is not defined | the same name everywhere |
A mistake with no message ๐ฌ
Click Next - the sneakiest kind of mistake.
Code
๐ฅ๏ธ Screen
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?
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.
1height_str = input()2height = int(height_str)3if height > 120:4print("TAK")5else:6print("NIE")
โSneaky bugs - the detective's list
These bugs give no message - the program runs, but answers wrong:
| Suspicious code | What's wrong | The fix |
|---|---|---|
if height > 120: for "from 120" | the limit doesn't count | >= |
if day == 6 or 7: | or 7 is always True | day == 6 or day == 7 |
if ans == 5: after ans = input() | text is never equal to a number | int(ans) == 5 |
if p >= 50: before elif p >= 90: | the first True wins too early | the highest limit first |
if n >= 1 or n <= 10: for "from 1 to 10" | almost any number fits | and 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.
1day = 32if day == 6 or 7:3print("Weekend!")4else:5print("School")
โ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.
1day = 32if day == 6 or day == 7:3print("Weekend!")4else:5print("School")
โ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 = 4if n == 4print("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.