Lesson 26
An if inside an if
After this lesson: You'll ask a second question only when the answer to the first one is True.
Video
The video for this lesson is coming soon.
Explanation
Any line can be one of an if's indented lines - even... another if! It's like a castle with chambers: first the gate, and only inside it the doors to the next rooms. ๐ฐ
A castle with chambers ๐ฌ
Click Next - the second question is asked only behind the first gate.
Code
๐ฅ๏ธ Screen
The castle gate
You enter the castle ๐ฐ with a ticket. Without one the guard says: "Buy a ticket first" - and nothing else happens.
The castle: Ola, age 10
A fork inside a fork: the robot goes through the gate, and inside it picks another road.
ticket = Trueage = 10print("You enter the castle ๐ฐ")print("You may go up the tower")print("Welcome to the garden ๐ท")print("Buy a ticket first")Click the arrow - I'll walk the program's path, line by line.
1ticket = True2age = 103if ticket:4print("You enter the castle ๐ฐ")5if age >= 12:6print("You may go up the tower")7else:8print("Welcome to the garden ๐ท")9else:10print("Buy a ticket first")
โThe castle: no ticket
The first gate is closed - the whole inside of the castle is grey, because the robot doesn't go in.
ticket = Falseage = 14print("You enter the castle ๐ฐ")print("You may go up the tower")print("Welcome to the garden ๐ท")print("Buy a ticket first")Click the arrow - I'll walk the program's path, line by line.
1ticket = False2age = 143if ticket:4print("You enter the castle ๐ฐ")5if age >= 12:6print("You may go up the tower")7else:8print("Welcome to the garden ๐ท")9else:10print("Buy a ticket first")
โLevels of indentation
| Line | Spaces | Belongs to |
|---|---|---|
if ticket: | 0 | the main program |
print("You enter...") | 4 | the indented lines of if ticket |
if age >= 12: | 4 | the indented lines of if ticket |
print("You may go...") | 8 | the indented lines of if age >= 12 |
else: (under if age) | 4 | the else of the inner if |
else: (under if ticket) | 0 | the else of the outer if |
Which else belongs to which if? The one standing in line with it - the same number of spaces.
The pool ๐
People from 140 cm ๐ may use the big pool. Type the height, then 1 if you can swim, or 0 if you can't - Enter โ after each number.
height_str = input()height = int(height_str)swims_str = input()swims = int(swims_str)print("The big pool ๐")print("A swimming lesson first")print("The paddling pool ๐ฆ")Click the arrow - I'll walk the program's path, line by line.
1height_str = input()2height = int(height_str)3swims_str = input()4swims = int(swims_str)5if height >= 140:6if swims == 1:7print("The big pool ๐")8else:9print("A swimming lesson first")10else:11print("The paddling pool ๐ฆ")
โThe biggest of three numbers ๐ฌ
Click Next - a tournament where every match is one if.
A tournament of three numbers
Three numbers a, b and c play a tournament: the biggest wins. First the match a against b.
The tournament on the podium
Watch which bars are compared - and where the crown ends up.
Click the arrow - the numbers will stand on the podium as bars.
1a = 42b = 93c = 74if a > b:5if a > c:6biggest = a7else:8biggest = c9else:10if b > c:11biggest = b12else:13biggest = c14print(biggest)
โThe tournament as a road
The same program - two levels of forks.
a = 4b = 9c = 7biggest = abiggest = cbiggest = bbiggest = cprint(biggest)Click the arrow - I'll walk the program's path, line by line.
1a = 42b = 93c = 74if a > b:5if a > c:6biggest = a7else:8biggest = c9else:10if b > c:11biggest = b12else:13biggest = c14print(biggest)
โAn if inside an if, or and?
Sometimes an if inside an if can be replaced with and - when there's no else inside:
if height >= 140:
if swims == 1:
print("The big pool")
is the same as:
if height >= 140 and swims == 1:
print("The big pool")
But when every situation has a different answer (the big pool, a swimming lesson, the paddling pool), an if inside an if is clearer - you can see every road separately.
A leap year ๐ฌ
Click Next - a real puzzle from the calendar, solved with an if inside an if.
An extra day
A leap year has 29 February - 366 days. The rule: a year is a leap year when it divides by 4... but with exceptions!
Is it a leap year? ๐
Type a year ๐ (e.g. 2024, 2026, 1900 or 2000) and press Enter โ - go through the three gates.
year_str = input()year = int(year_str)print("TAK")print("NIE")print("TAK")print("NIE")Click the arrow - I'll walk the program's path, line by line.
1year_str = input()2year = int(year_str)3if year % 4 == 0:4if year % 100 == 0:5if year % 400 == 0:6print("TAK")7else:8print("NIE")9else:10print("TAK")11else:12print("NIE")
โWhat will this program print?
x = 5if x > 0:if x > 10:print("A")else:print("B")else:print("C")
How many spaces does an indented line of an if have, when that if is itself indented inside another if?
Was 1900 a leap year?
Remember
- An if's indented lines can include another if - a question behind a gate.
- The inner if is checked only when the outer condition was True.
- Every level is 4 more spaces: 4, 8, 12...
- An else belongs to the if it stands in line with.
- With no else inside, an if inside an if can be replaced with and.
On to the tasks! Draw the gates and doors in your head - or on paper.
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.