AlgoBazaAlgoBaza
Python for kids ยท Module 2

Lesson 22

if and else - one of two

After this lesson: You'll write a program that always picks one of two roads: if or else.

Film to watch

Video

Two roads: if and else ๐ŸŽฌ

Play the film โ–ถ - you'll see that with if and else exactly one road always runs.

Loading the film...

?

A YouTube video for this lesson will appear here.

Explanation

An if on its own does something or nothing. But often we want to do one of two things: take an umbrella or sunglasses, TAK or NIE, Ola won or Kuba did. That's what the word else is for - otherwise. โ˜‚๏ธ๐Ÿ˜Ž

How an if-else is built

if temperature < 15:
    print("Put on a jacket")
else:
    print("A T-shirt is enough")
PieceWhat it means
if condition:the road when the condition is True
else:the road in every other case - no condition, with a colon
indentationeach road has its own indented lines
else in line with ifthe else belongs to that if

The road with a fork now has two lanes - and the robot always picks exactly one.

Jacket or T-shirt?

8 degrees - cold. The robot takes the if lane.

โ“ questionโš–๏ธ answer๐Ÿ›ฃ๏ธ choosing a road๐Ÿค together again
๐Ÿค–1temperature = 8
๐Ÿšฆ
2if temperature < 15:
Trueโžœ the if's indented lines

4 spaces = these lines belong to the if

3print("Put on a jacket ๐Ÿงฅ")
๐Ÿงฅ
Falseโžœ the lines under else
4else:
5print("A T-shirt is enough ๐Ÿ‘•")
๐Ÿ‘•
๐Ÿค the roads join
6print("You can go out!")

Click the arrow - ๐Ÿค– will walk the program's road, line by line.

Code
1temperature = 8
2if temperature < 15:
3 print("Put on a jacket ๐Ÿงฅ")
4else:
5 print("A T-shirt is enough ๐Ÿ‘•")
6print("You can go out!")
Screen
โ–
1/5 ยท line 1 runs next

A hot day

The same program for 27 degrees - this time the else lane.

โ“ questionโš–๏ธ answer๐Ÿ›ฃ๏ธ choosing a road๐Ÿค together again
๐Ÿค–1temperature = 27
๐Ÿšฆ
2if temperature < 15:
Trueโžœ the if's indented lines

4 spaces = these lines belong to the if

3print("Put on a jacket ๐Ÿงฅ")
๐Ÿงฅ
Falseโžœ the lines under else
4else:
5print("A T-shirt is enough ๐Ÿ‘•")
๐Ÿ‘•
๐Ÿค the roads join
6print("You can go out!")

Click the arrow - ๐Ÿค– will walk the program's road, line by line.

Code
1temperature = 27
2if temperature < 15:
3 print("Put on a jacket ๐Ÿงฅ")
4else:
5 print("A T-shirt is enough ๐Ÿ‘•")
6print("You can go out!")
Screen
โ–
1/5 ยท line 1 runs next

TAK or NIE - like at an olympiad ๐ŸŽฌ

Click Next - this is how you answer in contest tasks.

?Bajtek
๐Ÿ“œTAK or NIE?
1

A question in a task

In olympiad tasks (and in many of our tasks) you often see: "print TAK if the number is positive, otherwise NIE" (TAK is Polish for yes, NIE for no).

Step 1 / 3

TAK or NIE

For -3 the robot takes the else lane.

โ“ questionโš–๏ธ answer๐Ÿ›ฃ๏ธ choosing a road๐Ÿค together again
๐Ÿง‘โ€โš–๏ธ1n = -3
๐Ÿšฆ
2if n > 0:
Trueโžœ the if's indented lines

4 spaces = these lines belong to the if

3print("TAK")
โœ…
Falseโžœ the lines under else
4else:
5print("NIE")
โŒ
๐Ÿค the roads join

Click the arrow - ๐Ÿง‘โ€โš–๏ธ will walk the program's road, line by line.

Code
1n = -3
2if n > 0:
3 print("TAK")
4else:
5 print("NIE")
Screen
โ–
1/4 ยท line 1 runs next

Even or odd? ๐Ÿ”ข

Type a number ๐Ÿ”ข and press Enter โŽ. An even number divides by 2 with no remainder - more about it in lesson 24.

โ“ questionโš–๏ธ answer๐Ÿ›ฃ๏ธ choosing a road๐Ÿค together again
๐Ÿค–1n_str = input()
2n = int(n_str)
๐Ÿšฆ
3if n % 2 == 0:
Trueโžœ the if's indented lines

4 spaces = these lines belong to the if

4print("Even ๐Ÿ‘ฏ")
๐Ÿ‘ฏ
Falseโžœ the lines under else
5else:
6print("Odd ๐Ÿง")
๐Ÿง
๐Ÿค the roads join

Click the arrow - ๐Ÿค– will walk the program's road, line by line.

Code
1n_str = input()
2n = int(n_str)
3if n % 2 == 0:
4 print("Even ๐Ÿ‘ฏ")
5else:
6 print("Odd ๐Ÿง")
Screen
โ–
1/2 ยท line 1 runs next

Each road can calculate

The if and else roads can hold any lines - for example putting a value into a box. That's how we find the bigger of two numbers:

if a > b:
    bigger = a
else:
    bigger = b
print(bigger)

Each road puts something into the box bigger - so after the if-else the box is surely full.

The bigger of two on the podium

The bars are the numbers a and b - the crown goes on the bigger one.

?
๐Ÿง’
?
๐Ÿ‘ง
๐Ÿ“ฆ a๐Ÿ“ฆ b
?

Click the arrow - the numbers will stand on the podium as bars.

Code
1a = 4
2b = 9
3if a > b:
4 bigger = a
5else:
6 bigger = b
7print(bigger)
Screen
โ–
1/6 ยท line 1 runs next

Who has more apples? ๐ŸŽ

Type the number of apples Ola ๐Ÿ‘ง has, then Kuba ๐Ÿง’ - two different numbers, Enter โŽ after each.

โ“ questionโš–๏ธ answer๐Ÿ›ฃ๏ธ choosing a road๐Ÿค together again
๐ŸŽ1ola_str = input()
2ola = int(ola_str)
3kuba_str = input()
4kuba = int(kuba_str)
๐Ÿšฆ
5if ola > kuba:
Trueโžœ the if's indented lines

4 spaces = these lines belong to the if

6print("Ola has more")
๐Ÿ‘ง
Falseโžœ the lines under else
7else:
8print("Kuba has more")
๐Ÿง’
๐Ÿค the roads join

Click the arrow - ๐ŸŽ will walk the program's road, line by line.

Code
1ola_str = input()
2ola = int(ola_str)
3kuba_str = input()
4kuba = int(kuba_str)
5if ola > kuba:
6 print("Ola has more")
7else:
8 print("Kuba has more")
Screen
โ–
1/2 ยท line 1 runs next

And when they both have the same?

Try typing two equal numbers in the previous example. The program says "Kuba has more" - and that's not true! When ola > kuba is False, else always runs - also in a draw. How do we add a third road "A draw"? That's in lesson 23!

Traps with else

CodeWhat's wrong
elseno colon
else n < 0:else has no condition
else: indented differently from ifelse must stand in line with the if
something between the if's indented lines and elseelse must come right after the if's indented lines
?

What will this program print?

x = 5
if x > 10:
print("A")
else:
print("B")
print("C")
?

Which else line is correct?

?

How many roads of an if-else can run at once?

Remember

  • else: is the road for every other case - when the if's condition is False.
  • else has no condition, it has a colon and stands in line with the if.
  • With if-else exactly one road always runs.
  • if raining: - the condition can be a variable holding True or False.
  • We print the answer TAK / NIE exactly like that - in capital letters, no full stop.

The tasks are waiting! In many of them the answer is TAK or NIE.

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.