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")
| Piece | What it means |
|---|---|
if condition: | the road when the condition is True |
else: | the road in every other case - no condition, with a colon |
| indentation | each road has its own indented lines |
else in line with if | the 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.
temperature = 8if temperature < 15:4 spaces = these lines belong to the if
print("Put on a jacket ๐งฅ")else:print("A T-shirt is enough ๐")print("You can go out!")Click the arrow - ๐ค will walk the program's road, line by line.
1temperature = 82if temperature < 15:3print("Put on a jacket ๐งฅ")4else:5print("A T-shirt is enough ๐")6print("You can go out!")
โA hot day
The same program for 27 degrees - this time the else lane.
temperature = 27if temperature < 15:4 spaces = these lines belong to the if
print("Put on a jacket ๐งฅ")else:print("A T-shirt is enough ๐")print("You can go out!")Click the arrow - ๐ค will walk the program's road, line by line.
1temperature = 272if temperature < 15:3print("Put on a jacket ๐งฅ")4else:5print("A T-shirt is enough ๐")6print("You can go out!")
โTAK or NIE - like at an olympiad ๐ฌ
Click Next - this is how you answer in contest tasks.
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).
TAK or NIE
For -3 the robot takes the else lane.
n = -3if n > 0:4 spaces = these lines belong to the if
print("TAK")else:print("NIE")Click the arrow - ๐งโโ๏ธ will walk the program's road, line by line.
1n = -32if n > 0:3print("TAK")4else:5print("NIE")
โEven or odd? ๐ข
Type a number ๐ข and press Enter โ. An even number divides by 2 with no remainder - more about it in lesson 24.
n_str = input()n = int(n_str)if n % 2 == 0:4 spaces = these lines belong to the if
print("Even ๐ฏ")else:print("Odd ๐ง")Click the arrow - ๐ค will walk the program's road, line by line.
1n_str = input()2n = int(n_str)3if n % 2 == 0:4print("Even ๐ฏ")5else:6print("Odd ๐ง")
โ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.
Click the arrow - the numbers will stand on the podium as bars.
1a = 42b = 93if a > b:4bigger = a5else:6bigger = b7print(bigger)
โWho has more apples? ๐
Type the number of apples Ola ๐ง has, then Kuba ๐ง - two different numbers, Enter โ after each.
ola_str = input()ola = int(ola_str)kuba_str = input()kuba = int(kuba_str)if ola > kuba:4 spaces = these lines belong to the if
print("Ola has more")else:print("Kuba has more")Click the arrow - ๐ will walk the program's road, line by line.
1ola_str = input()2ola = int(ola_str)3kuba_str = input()4kuba = int(kuba_str)5if ola > kuba:6print("Ola has more")7else:8print("Kuba has more")
โ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
| Code | What's wrong |
|---|---|
else | no colon |
else n < 0: | else has no condition |
else: indented differently from if | else must stand in line with the if |
something between the if's indented lines and else | else must come right after the if's indented lines |
What will this program print?
x = 5if 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.
