AlgoBazaAlgoBaza
Python for kids ยท Module 2

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.

Bajtek
โ„๏ธ๐Ÿงฅ๐Ÿ™‚๐Ÿ”ฅ
1

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 ๐Ÿ”ฅ.

Step 1 / 5

The thermometer: 18 degrees

Watch the robot check the signs in turn and stop at the first green one.

๐ŸŒก๏ธ1temp = 18
if temp < 0
3print("Frost โ„๏ธ")
โ„๏ธ
elif temp < 15
5print("Cool ๐Ÿงฅ")
๐Ÿงฅ
elif temp < 25
7print("Warm ๐Ÿ™‚")
๐Ÿ™‚
elsein every other case
9print("Heat ๐Ÿ”ฅ")
๐Ÿ”ฅ

Click the arrow - I'll walk the program's path, line by line.

Code
1temp = 18
2if temp < 0:
3 print("Frost โ„๏ธ")
4elif temp < 15:
5 print("Cool ๐Ÿงฅ")
6elif temp < 25:
7 print("Warm ๐Ÿ™‚")
8else:
9 print("Heat ๐Ÿ”ฅ")
Screen
โ–
1/6 ยท line 1 runs next

Your thermometer ๐ŸŒก๏ธ

Type a temperature ๐ŸŒก๏ธ (e.g. -5, 10 or 30) and press Enter โŽ. Check the limits too: 15 and 25!

๐ŸŒก๏ธ1temp_str = input()
2temp = int(temp_str)
if temp < 0
4print("Frost โ„๏ธ")
โ„๏ธ
elif temp < 15
6print("Cool ๐Ÿงฅ")
๐Ÿงฅ
elif temp < 25
8print("Warm ๐Ÿ™‚")
๐Ÿ™‚
elsein every other case
10print("Heat ๐Ÿ”ฅ")
๐Ÿ”ฅ

Click the arrow - I'll walk the program's path, line by line.

Code
1temp_str = input()
2temp = int(temp_str)
3if temp < 0:
4 print("Frost โ„๏ธ")
5elif temp < 15:
6 print("Cool ๐Ÿงฅ")
7elif temp < 25:
8 print("Warm ๐Ÿ™‚")
9else:
10 print("Heat ๐Ÿ”ฅ")
Screen
โ–
1/2 ยท line 1 runs next

How an elif is built

PieceHow manyWhat it means
if condition:exactly 1, at the startthe first sign
elif condition:as many as you likeotherwise, maybe... - the next sign with a condition
else:0 or 1, at the endeverything 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.

Bajtek
That's not fair!๐Ÿ˜ 

Code

ola = 6
kuba = 6
if ola > kuba:
print("Ola wins")
else:
print("Kuba wins")

๐Ÿ–ฅ๏ธ Screen

Kuba wins
โŒ the wrong answer
1

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!

Step 1 / 3

Who won the match? ๐Ÿ€

Type Ola's ๐Ÿ‘ง points, then Kuba's ๐Ÿง’ - Enter โŽ after each number. Try two equal numbers too!

๐Ÿ€1ola_str = input()
2ola = int(ola_str)
3kuba_str = input()
4kuba = int(kuba_str)
if ola > kuba
6print("Ola wins")
๐Ÿ‘ง
elif kuba > ola
8print("Kuba wins")
๐Ÿง’
elsein every other case
10print("A draw ๐Ÿค")
๐Ÿค

Click the arrow - I'll walk the program's path, line by line.

Code
1ola_str = input()
2ola = int(ola_str)
3kuba_str = input()
4kuba = int(kuba_str)
5if ola > kuba:
6 print("Ola wins")
7elif kuba > ola:
8 print("Kuba wins")
9else:
10 print("A draw ๐Ÿค")
Screen
โ–
1/2 ยท line 1 runs next

School marks

At school the points from a test are turned into a mark:

PointsMark
90 and more5
75 - 894
50 - 743
less than 502

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.

๐Ÿ“1points = 82
if points >= 90
3mark = 5
5๏ธโƒฃ
elif points >= 75
5mark = 4
4๏ธโƒฃ
elif points >= 50
7mark = 3
3๏ธโƒฃ
elsein every other case
9mark = 2
2๏ธโƒฃ
10print(mark)

Click the arrow - I'll walk the program's path, line by line.

Code
1points = 82
2if points >= 90:
3 mark = 5
4elif points >= 75:
5 mark = 4
6elif points >= 50:
7 mark = 3
8else:
9 mark = 2
10print(mark)
Screen
โ–
1/6 ยท line 1 runs next

The order matters ๐ŸŽฌ

Click Next - one sign in the wrong place gives the wrong mark.

?Bajtek

Code

points = 95
if points >= 50:
mark = 3
elif points >= 90:
mark = 5

๐Ÿ–ฅ๏ธ Screen

1

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.

Step 1 / 3

The wrong order in action

The robot stops at the first green sign - and never reaches >= 90.

๐Ÿ“1points = 95
if points >= 50
3mark = 3
3๏ธโƒฃ
elif points >= 90
5mark = 5
5๏ธโƒฃ
elsein every other case
7mark = 2
2๏ธโƒฃ
8print(mark)

Click the arrow - I'll walk the program's path, line by line.

Code
1points = 95
2if points >= 50:
3 mark = 3
4elif points >= 90:
5 mark = 5
6else:
7 mark = 2
8print(mark)
Screen
โ–
1/5 ยท line 1 runs next
?

What will this program print?

n = 0
if n > 0:
print("plus")
elif n < 0:
print("minus")
else:
print("zero")
?

What will this program print?

x = 20
if 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.