AlgoBazaAlgoBaza
Python for kids ยท Module 2

Lesson 21

if - when the condition is true

After this lesson: You'll write a program that runs lines only when a condition is true.

Video

?

The video for this lesson is coming soon.

Explanation

You can already ask questions: age >= 12 gives True or False. Today is the most important lesson of the module: the program will decide by itself whether to run a line - based on the answer! The word for it is if. ๐Ÿšฆ

Bajtek guards the rollercoaster ๐ŸŽฌ

Click Next - you'll see how if decides which line runs.

Bajtek
๐ŸŽกโ˜๏ธ
๐Ÿšƒ
๐ŸŽข 12+
๐Ÿงข
13๐Ÿ‘งโœ”
9๐Ÿง’โœ˜
15๐Ÿ‘ฆโœ”
ifage >= 12โžœ๐ŸŽข you get on
1

Bajtek's new job

At the funfair Bajtek guards the entrance to the rollercoaster ๐ŸŽข. The rule is simple: if you are 12 or older, you may get on.

Step 1 / 6

How an if is built

if age >= 12:
    print("Get on!")
PieceWhat it means
ifif - the decision starts here
age >= 12the condition - a question that gives True or False
:a colon - "and now what to do"
4 spaces before printindentation - this line belongs to the if

Below is the same program as a road with a fork: the robot walks from the top, and at the if sign it checks the condition. Green road - it goes that way, grey - it goes around.

Ola, age 13

The condition is true - the robot turns into the if road.

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

4 spaces = these lines belong to the if

3print("Get on!")
๐ŸŽข
Falseโžœ go around - do nothing
โžก๏ธ
๐Ÿค the roads join
4print("Have fun!")

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

Code
1age = 13
2if age >= 12:
3 print("Get on!")
4print("Have fun!")
Screen
โ–
1/5 ยท line 1 runs next

Kuba, age 9

The same program, a different number. The condition is false - the robot walks around the indented line.

โ“ questionโš–๏ธ answer๐Ÿ›ฃ๏ธ choosing a road๐Ÿค together again
๐Ÿง’1age = 9
๐Ÿšฆ
2if age >= 12:
Trueโžœ the if's indented lines

4 spaces = these lines belong to the if

3print("Get on!")
๐ŸŽข
Falseโžœ go around - do nothing
โžก๏ธ
๐Ÿค the roads join
4print("Have fun!")

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

Code
1age = 9
2if age >= 12:
3 print("Get on!")
4print("Have fun!")
Screen
โ–
1/4 ยท line 1 runs next

Two indented lines

Both indented lines belong to the if - they run together or not at all.

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

4 spaces = these lines belong to the if

3print("Well done!")
4print("You get a prize ๐Ÿ†")
๐Ÿ†
Falseโžœ go around - do nothing
โžก๏ธ
๐Ÿค the roads join
5print("Game over")

Click the arrow - ๐Ÿ•น๏ธ will walk the program's road, line by line.

Code
1points = 15
2if points > 10:
3 print("Well done!")
4 print("You get a prize ๐Ÿ†")
5print("Game over")
Screen
โ–
1/6 ยท line 1 runs next

The rollercoaster gate ๐ŸŽข

Type your age ๐ŸŽ‚ and press Enter โŽ. Try 11 and 12 - see which one opens the gate!

โ“ questionโš–๏ธ answer๐Ÿ›ฃ๏ธ choosing a road๐Ÿค together again
๐Ÿง’1age_str = input()
2age = int(age_str)
๐Ÿšฆ
3if age >= 12:
Trueโžœ the if's indented lines

4 spaces = these lines belong to the if

4print("Get on! ๐ŸŽข")
๐ŸŽข
Falseโžœ go around - do nothing
โžก๏ธ
๐Ÿค the roads join
5print("Have fun!")

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

Code
1age_str = input()
2age = int(age_str)
3if age >= 12:
4 print("Get on! ๐ŸŽข")
5print("Have fun!")
Screen
โ–
1/2 ยท line 1 runs next

if changes a value

Any line can be inside an if - not only print. For example, changing the value in a box. In Bajtek's game a player who collects 100 points gets a bonus of 50:

if points >= 100:
    points = points + 50
print(points)

If the condition is false, the box stays the same - and print prints the result anyway.

A bonus for 100 points

The player has 120 points - the condition is true, so they get the bonus.

โ“ questionโš–๏ธ answer๐Ÿ›ฃ๏ธ choosing a road๐Ÿค together again
๐Ÿ•น๏ธ1points = 120
๐Ÿšฆ
2if points >= 100:
Trueโžœ the if's indented lines

4 spaces = these lines belong to the if

3points = points + 50
โญ
Falseโžœ go around - do nothing
โžก๏ธ
๐Ÿค the roads join
4print(points)

Click the arrow - ๐Ÿ•น๏ธ will walk the program's road, line by line.

Code
1points = 120
2if points >= 100:
3 points = points + 50
4print(points)
Screen
โ–
1/5 ยท line 1 runs next

Bajtek's safe ๐Ÿ”

The safe opens only for the code 1234. Type a code ๐Ÿ”ข and press Enter โŽ - try a wrong one and the right one!

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

4 spaces = these lines belong to the if

4print("The safe is open! ๐Ÿ’ฐ")
๐Ÿ’ฐ
Falseโžœ go around - do nothing
โžก๏ธ
๐Ÿค the roads join
5print("Goodbye")

Click the arrow - ๐Ÿ•ต๏ธ will walk the program's road, line by line.

Code
1code_str = input()
2code = int(code_str)
3if code == 1234:
4 print("The safe is open! ๐Ÿ’ฐ")
5print("Goodbye")
Screen
โ–
1/2 ยท line 1 runs next

Three traps

With an if, three mistakes happen most often:

  1. Forgotten indentation - the line under the if stands at the edge.
  2. No colon - there's no : at the end of the if line.
  3. = instead of == - putting into a box instead of asking a question.

Let's look at each of them in turn.

Trap 1: Forgotten indentation

After the colon Python waits for an indented line. If print stands at the edge, Python doesn't know what belongs to the if, and stops with the error IndentationError: expected an indented block - I was waiting for an indented line.

We indent with four spaces (or the Tab key - most editors turn it into 4 spaces).

Trap 1: forgotten indentation

Python waits for the if's indented lines - and there aren't any.

Code
1if 5 > 3:
2print("Tak")
Screen
File "<lesson>", line 2
    print("Tak")
    ^^^^^
IndentationError: expected an indented block after 'if' statement on line 1
1/1 ยท the program stopped with an error

Trap 2: No colon

The colon : ends the if's question. Without it Python doesn't know where the question ends, and already at the if line it says SyntaxError - a spelling mistake.

Trap 2: no colon

The colon is missing at the end of the if line.

Code
1age = 13
2if age >= 12
3 print("Get on!")
Screen
File "<lesson>", line 2
    if age >= 12
                ^
SyntaxError: expected ':'
1/1 ยท the program stopped with an error

Trap 3: = instead of ==

A single = means putting into a box: age = 12. But an if needs a question: age == 12 - is age equal to 12? With a single = Python says SyntaxError.

Trap 3: = instead of ==

The if has a single = - but it should be the question ==.

Code
1age = 12
2if age = 12:
3 print("You are 12")
Screen
File "<lesson>", line 2
    if age = 12:
       ^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1/1 ยท the program stopped with an error
?

What will this program print?

x = 4
if x > 10:
print("a lot")
print("the end")
?

Which if line is correct?

?

What will this program print?

n = 7
if n > 5:
n = n * 2
print(n)

Remember

  • if condition: - if the condition is True, run the indented lines; if False - jump over them.
  • An if ends with a colon :.
  • The if's lines are indented by 4 spaces - all in line.
  • Lines without indentation don't belong to the if - they always run.
  • Any line can be in an if: print, changing a variable...

Time for the tasks! In each one the program always prints something, and the if adds or changes something only sometimes.

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.