Look first: if · Robot Quest
How can one program handle different situations?
Course contentsWhy you need it
So far every program was written for one map. Move one gem and the program breaks. Real programs face data they have never seen. To cope, a program must look at the situation and decide: that is what if does.
The intuition
"If it's raining, take an umbrella." You check a condition, and only act when it's true.
Bit can now answer questions. Each answer is True or False:
on_gem(): am I standing on a gem?front_clear(): is the square ahead free (no wall)?at_goal(): am I on the flag?
if on_gem():
pick()The indented line runs only when the answer is True. Add else: for what to do otherwise:
if front_clear():
move()
else:
turn_left()Definition
Condition
A condition is an expression whose value is True or False; an if statement runs its block only when the condition is True.
Boolean
A Boolean is a value that is either True or False.
Examples
Pick only where there is a gem
Run it in the lessonThe same code works wherever the gems are. Try moving the gems: edit the map? You can't, but the next exercise will test other maps.
for i in range(7):
move()
if on_gem():
pick()- L2Move one square.
- L3Ask: on a gem? Here the answer is False, so line 4 is skipped.
on_gem() → False - L3Next round, on the gem square: True.
on_gem() → True - L4Only now does pick() run.
Turn at the wall
Run it in the lessonif ... else chooses between two actions every round.
for i in range(5):
if front_clear():
move()
else:
turn_left()- L2Front is clear: True.
front_clear() → True - L3So Bit moves. (The else part is skipped.)
- L2At the corner the wall is ahead: False.
front_clear() → False - L5So the else part runs: turn left.
- L3Facing up, the front is clear again: two more moves to the flag.
Common mistakes
Forgetting the brackets on a question
if on_gem: (no brackets) is always treated as true, because you didn't ask the question, you only named it. Write if on_gem():.
else needs its own colon
Write else: at the same indentation as its if, followed by an indented block.
Quick quiz
1. What kind of value does on_gem() give back?
Show the answer
Answer: A
It is a yes/no question, so the answer is a Boolean.
2. Where does Bit end up?
for i in range(3):
if on_gem():
pick()
move()Show the answer
Answer: B
The check happens before each move. After the last move Bit stands on the second gem, but the loop is over, so nobody checks again.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictDecide each round
Predict the result.
- ModifyGems anywhere
This program assumed a gem on every square. Now the gems are in different places on each of 3 maps. Make one program that works on all of them.
- WriteWhere is the corner?
Each map is an L: east, then up. The corner is in a different place each time, and there may be gems. The path is always 6 squares long. Write one program for all 3 maps.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| if statement | if 语句 | A statement that runs a block only when its condition is True. |
| Condition | 条件 | An expression that is either True or False. |
| Boolean | 布尔值 | A value that is either True or False. |
| else | else 分支 | The block that runs when the if condition is False. |