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'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.
How an if is built
if age >= 12:
print("Get on!")
| Piece | What it means |
|---|---|
if | if - the decision starts here |
age >= 12 | the condition - a question that gives True or False |
: | a colon - "and now what to do" |
4 spaces before print | indentation - 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.
age = 13if age >= 12:4 spaces = these lines belong to the if
print("Get on!")print("Have fun!")Click the arrow - ๐ง will walk the program's road, line by line.
1age = 132if age >= 12:3print("Get on!")4print("Have fun!")
โKuba, age 9
The same program, a different number. The condition is false - the robot walks around the indented line.
age = 9if age >= 12:4 spaces = these lines belong to the if
print("Get on!")print("Have fun!")Click the arrow - ๐ง will walk the program's road, line by line.
1age = 92if age >= 12:3print("Get on!")4print("Have fun!")
โTwo indented lines
Both indented lines belong to the if - they run together or not at all.
points = 15if points > 10:4 spaces = these lines belong to the if
print("Well done!")print("You get a prize ๐")print("Game over")Click the arrow - ๐น๏ธ will walk the program's road, line by line.
1points = 152if points > 10:3print("Well done!")4print("You get a prize ๐")5print("Game over")
โThe rollercoaster gate ๐ข
Type your age ๐ and press Enter โ. Try 11 and 12 - see which one opens the gate!
age_str = input()age = int(age_str)if age >= 12:4 spaces = these lines belong to the if
print("Get on! ๐ข")print("Have fun!")Click the arrow - ๐ง will walk the program's road, line by line.
1age_str = input()2age = int(age_str)3if age >= 12:4print("Get on! ๐ข")5print("Have fun!")
โ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.
points = 120if points >= 100:4 spaces = these lines belong to the if
points = points + 50print(points)Click the arrow - ๐น๏ธ will walk the program's road, line by line.
1points = 1202if points >= 100:3points = points + 504print(points)
โ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!
code_str = input()code = int(code_str)if code == 1234:4 spaces = these lines belong to the if
print("The safe is open! ๐ฐ")print("Goodbye")Click the arrow - ๐ต๏ธ will walk the program's road, line by line.
1code_str = input()2code = int(code_str)3if code == 1234:4print("The safe is open! ๐ฐ")5print("Goodbye")
โThree traps
With an if, three mistakes happen most often:
- Forgotten indentation - the line under the if stands at the edge.
- No colon - there's no
:at the end of the if line. =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.
1if 5 > 3:2print("Tak")
File "<lesson>", line 2
print("Tak")
^^^^^
IndentationError: expected an indented block after 'if' statement on line 1Trap 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.
1age = 132if age >= 123print("Get on!")
File "<lesson>", line 2
if age >= 12
^
SyntaxError: expected ':'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 ==.
1age = 122if age = 12:3print("You are 12")
File "<lesson>", line 2
if age = 12:
^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?What will this program print?
x = 4if x > 10:print("a lot")print("the end")
Which if line is correct?
What will this program print?
n = 7if n > 5:n = n * 2print(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.