Everyone Can Learn Program
中文 Start learning
Course 1 · Zero to codingLesson 7 of 10Free to read

Look first: if · Robot Quest

How can one program handle different situations?

Course contents

Why 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 lesson

The 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()
Step by step
  1. L2Move one square.
  2. L3Ask: on a gem? Here the answer is False, so line 4 is skipped.on_gem() → False
  3. L3Next round, on the gem square: True.on_gem() → True
  4. L4Only now does pick() run.

Turn at the wall

Run it in the lesson

if ... else chooses between two actions every round.

for i in range(5):
    if front_clear():
        move()
    else:
        turn_left()
Step by step
  1. L2Front is clear: True.front_clear() → True
  2. L3So Bit moves. (The else part is skipped.)
  3. L2At the corner the wall is ahead: False.front_clear() → False
  4. L5So the else part runs: turn left.
  5. 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?

  1. ATrue or False
  2. BThe number of gems
  3. CNothing
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()
  1. AOn the flag
  2. BOn the second gem, which is not picked
  3. CError: no gem here
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.

  1. PredictDecide each round

    Predict the result.

  2. 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.

  3. 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

TermChineseDefinition
if statementif 语句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.
elseelse 分支The block that runs when the if condition is False.