Lesson 23
elif - many options
After this lesson: You'll write a program with many roads and understand why the order of the conditions matters.
Video
The video for this lesson is coming soon.
Explanation
if-else is two roads. But what if there are more options? The weather can be freezing, cool, warm or hot. And a match can be won by Ola, by Kuba - or be a draw! That's what elif is for - short for else if. ๐ก๏ธ
Bajtek's thermometer ๐ฌ
Click Next - you'll see how Python checks the signs in turn, from the top.
Four kinds of weather
Bajtek builds a thermometer that says what to wear. Below zero - frost โ๏ธ. Below 15 - cool ๐งฅ. Below 25 - warm ๐. And above - heat ๐ฅ.
The thermometer: 18 degrees
Watch the robot check the signs in turn and stop at the first green one.
temp = 18print("Frost โ๏ธ")print("Cool ๐งฅ")print("Warm ๐")print("Heat ๐ฅ")Click the arrow - I'll walk the program's path, line by line.
1temp = 182if temp < 0:3print("Frost โ๏ธ")4elif temp < 15:5print("Cool ๐งฅ")6elif temp < 25:7print("Warm ๐")8else:9print("Heat ๐ฅ")
โYour thermometer ๐ก๏ธ
Type a temperature ๐ก๏ธ (e.g. -5, 10 or 30) and press Enter โ. Check the limits too: 15 and 25!
temp_str = input()temp = int(temp_str)print("Frost โ๏ธ")print("Cool ๐งฅ")print("Warm ๐")print("Heat ๐ฅ")Click the arrow - I'll walk the program's path, line by line.
1temp_str = input()2temp = int(temp_str)3if temp < 0:4print("Frost โ๏ธ")5elif temp < 15:6print("Cool ๐งฅ")7elif temp < 25:8print("Warm ๐")9else:10print("Heat ๐ฅ")
โHow an elif is built
| Piece | How many | What it means |
|---|---|---|
if condition: | exactly 1, at the start | the first sign |
elif condition: | as many as you like | otherwise, maybe... - the next sign with a condition |
else: | 0 or 1, at the end | everything else - no condition |
They all stand in line with each other, and each has its own indented lines. Python checks from the top and runs the lines of the first true condition - the rest is skipped.
A draw! ๐ฌ
Remember the wrong apples from the last lesson? Click Next - we'll fix it with a third road.
Code
๐ฅ๏ธ Screen
A basketball match
Ola scored 6 baskets ๐, and Kuba 6 too. The program with if-else says: "Kuba wins" - because 6 > 6 is False and else runs. But that's a draw!
Who won the match? ๐
Type Ola's ๐ง points, then Kuba's ๐ง - Enter โ after each number. Try two equal numbers too!
ola_str = input()ola = int(ola_str)kuba_str = input()kuba = int(kuba_str)print("Ola wins")print("Kuba wins")print("A draw ๐ค")Click the arrow - I'll walk the program's path, line by line.
1ola_str = input()2ola = int(ola_str)3kuba_str = input()4kuba = int(kuba_str)5if ola > kuba:6print("Ola wins")7elif kuba > ola:8print("Kuba wins")9else:10print("A draw ๐ค")
โSchool marks
At school the points from a test are turned into a mark:
| Points | Mark |
|---|---|
| 90 and more | 5 |
| 75 - 89 | 4 |
| 50 - 74 | 3 |
| less than 50 | 2 |
In Python we write it starting from the highest limit:
The mark for 82 points
82 >= 90? No. 82 >= 75? Yes - mark 4, and the rest is skipped.
points = 82mark = 5mark = 4mark = 3mark = 2print(mark)Click the arrow - I'll walk the program's path, line by line.
1points = 822if points >= 90:3mark = 54elif points >= 75:5mark = 46elif points >= 50:7mark = 38else:9mark = 210print(mark)
โThe order matters ๐ฌ
Click Next - one sign in the wrong place gives the wrong mark.
Code
๐ฅ๏ธ Screen
Kuba has 95 points
Kuba has 95 points in a test - that's a 5! But Kuba's program starts from the lowest limit: first points >= 50.
The wrong order in action
The robot stops at the first green sign - and never reaches >= 90.
points = 95mark = 3mark = 5mark = 2print(mark)Click the arrow - I'll walk the program's path, line by line.
1points = 952if points >= 50:3mark = 34elif points >= 90:5mark = 56else:7mark = 28print(mark)
โWhat will this program print?
n = 0if n > 0:print("plus")elif n < 0:print("minus")else:print("zero")
What will this program print?
x = 20if x > 5:print(1)elif x > 10:print(2)else:print(3)
How many times can elif appear?
Remember
- elif condition: - otherwise, maybe... - the next sign with a condition.
- The order: first if, then any number of elifs, at the end (maybe) else.
- Python checks from the top and runs only the first true condition.
- When the limits are inside each other, start from the highest one (or the lowest with
<). - Three results (win, loss, draw) - a job for if, elif, else.
Now tasks with many roads! Remember the order of the signs.
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.