AlgoBazaAlgoBaza
Python for kids ยท Module 2

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.

Bajtek
๐ŸŽŸ๏ธโžก๏ธ๐Ÿฐ

Code

if ticket:
print("You enter the castle")
else:
print("Buy a ticket first")

๐Ÿ–ฅ๏ธ Screen

1

The castle gate

You enter the castle ๐Ÿฐ with a ticket. Without one the guard says: "Buy a ticket first" - and nothing else happens.

Step 1 / 5

The castle: Ola, age 10

A fork inside a fork: the robot goes through the gate, and inside it picks another road.

๐Ÿ‘ง1ticket = True
2age = 10
if ticket
4print("You enter the castle ๐Ÿฐ")
if age >= 12
6print("You may go up the tower")
๐Ÿ—ผ
elsein every other case
8print("Welcome to the garden ๐ŸŒท")
๐ŸŒท
๐Ÿฐ
elsein every other case
10print("Buy a ticket first")
๐ŸŽŸ๏ธ

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

Code
1ticket = True
2age = 10
3if ticket:
4 print("You enter the castle ๐Ÿฐ")
5 if age >= 12:
6 print("You may go up the tower")
7 else:
8 print("Welcome to the garden ๐ŸŒท")
9else:
10 print("Buy a ticket first")
Screen
โ–
1/7 ยท line 1 runs next

The castle: no ticket

The first gate is closed - the whole inside of the castle is grey, because the robot doesn't go in.

๐Ÿง’1ticket = False
2age = 14
if ticket
4print("You enter the castle ๐Ÿฐ")
if age >= 12
6print("You may go up the tower")
๐Ÿ—ผ
elsein every other case
8print("Welcome to the garden ๐ŸŒท")
๐ŸŒท
๐Ÿฐ
elsein every other case
10print("Buy a ticket first")
๐ŸŽŸ๏ธ

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

Code
1ticket = False
2age = 14
3if ticket:
4 print("You enter the castle ๐Ÿฐ")
5 if age >= 12:
6 print("You may go up the tower")
7 else:
8 print("Welcome to the garden ๐ŸŒท")
9else:
10 print("Buy a ticket first")
Screen
โ–
1/5 ยท line 1 runs next

Levels of indentation

LineSpacesBelongs to
if ticket:0the main program
print("You enter...")4the indented lines of if ticket
if age >= 12:4the indented lines of if ticket
print("You may go...")8the indented lines of if age >= 12
else: (under if age)4the else of the inner if
else: (under if ticket)0the 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.

๐ŸŠ1height_str = input()
2height = int(height_str)
3swims_str = input()
4swims = int(swims_str)
if height >= 140
if swims == 1
7print("The big pool ๐ŸŠ")
๐Ÿ›Ÿ
elsein every other case
9print("A swimming lesson first")
๐Ÿฆ†
๐ŸŠ
elsein every other case
11print("The paddling pool ๐Ÿฆ†")

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

Code
1height_str = input()
2height = int(height_str)
3swims_str = input()
4swims = int(swims_str)
5if height >= 140:
6 if swims == 1:
7 print("The big pool ๐ŸŠ")
8 else:
9 print("A swimming lesson first")
10else:
11 print("The paddling pool ๐Ÿฆ†")
Screen
โ–
1/2 ยท line 1 runs next

The biggest of three numbers ๐ŸŽฌ

Click Next - a tournament where every match is one if.

Bajtek
a = 4b = 9c = 7
1

A tournament of three numbers

Three numbers a, b and c play a tournament: the biggest wins. First the match a against b.

Step 1 / 3

The tournament on the podium

Watch which bars are compared - and where the crown ends up.

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

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

Code
1a = 4
2b = 9
3c = 7
4if a > b:
5 if a > c:
6 biggest = a
7 else:
8 biggest = c
9else:
10 if b > c:
11 biggest = b
12 else:
13 biggest = c
14print(biggest)
Screen
โ–
1/8 ยท line 1 runs next

The tournament as a road

The same program - two levels of forks.

๐Ÿ†1a = 4
2b = 9
3c = 7
if a > b
if a > c
6biggest = a
elsein every other case
8biggest = c
elsein every other case
if b > c
11biggest = b
elsein every other case
13biggest = c
14print(biggest)

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

Code
1a = 4
2b = 9
3c = 7
4if a > b:
5 if a > c:
6 biggest = a
7 else:
8 biggest = c
9else:
10 if b > c:
11 biggest = b
12 else:
13 biggest = c
14print(biggest)
Screen
โ–
1/8 ยท line 1 runs next

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.

?Bajtek
๐Ÿ“…29 February
1

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!

Step 1 / 3

Is it a leap year? ๐Ÿ“…

Type a year ๐Ÿ“… (e.g. 2024, 2026, 1900 or 2000) and press Enter โŽ - go through the three gates.

๐Ÿ“…1year_str = input()
2year = int(year_str)
if year % 4 == 0
if year % 100 == 0
if year % 400 == 0
6print("TAK")
elsein every other case
8print("NIE")
elsein every other case
10print("TAK")
elsein every other case
12print("NIE")

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

Code
1year_str = input()
2year = int(year_str)
3if year % 4 == 0:
4 if year % 100 == 0:
5 if year % 400 == 0:
6 print("TAK")
7 else:
8 print("NIE")
9 else:
10 print("TAK")
11else:
12 print("NIE")
Screen
โ–
1/2 ยท line 1 runs next
?

What will this program print?

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