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

Patterns in a loop · Robot Quest

How do you find the part of a task that repeats?

Course contents

Why you need it

Real tasks rarely repeat a single command. More often a group of commands repeats: "step, pick, step, pick". Spotting that group, the pattern, is the key skill for writing loops. Once you see it, the loop almost writes itself.

The intuition

Stairs: "step forward, step up" again and again. One step of the stairs is the pattern:

move()        # forward
turn_left()   # face up
move()        # up
turn_right()  # face forward again

Put those four lines in the loop body, and range(3) climbs three steps.

A good test: after one round of the pattern, is Bit in the same situation as at the start (same direction, next step ahead)? If yes, the pattern can repeat.

Definition

Pattern

A pattern is a group of steps that repeats; it becomes the body of a loop.

Examples

The pattern is: move, pick. The last move() is outside the loop.

for i in range(5):
    move()
    pick()
move()
Step by step
  1. L2Round 1: move onto the first gem...i = 0
  2. L3...and pick it. One round = one gem.i = 0
  3. L2Round 2 starts in the same situation: facing east, a gem ahead.i = 1
  4. L4After 5 rounds all gems are collected; one last move reaches the flag.

Climbing stairs

Run it in the lesson

Four lines make one step. Three rounds climb three steps.

for i in range(3):
    move()
    turn_left()
    move()
    turn_right()
Step by step
  1. L2Forward one square.
  2. L3Turn to face up.
  3. L4Up one square.
  4. L5Turn back to face forward: the same situation as at the start of the round.
  5. •After round 3 Bit stands on the flag.

Common mistakes

The round doesn't end where it started

If one round leaves Bit facing a different way, the next round goes wrong. Check the direction at the end of each round.

Off by one

Often the first or last step is different (like the final move() to the flag). Put it before or after the loop.

Quick quiz

1. Where does Bit end up?

for i in range(3):
    move()
    move()
  1. AOn the flag
  2. BHalfway, 3 squares from the start
  3. CIt crashes
Show the answer

Answer: A

3 rounds × 2 moves = 6 moves, exactly the distance to the flag.

2. Where does Bit end up?

for i in range(2):
    move()
    turn_right()
  1. AMiddle square of the room
  2. BTop-right corner
  3. COn the flag
Show the answer

Answer: A

Round 1: east, then face south. Round 2: south, then face west. Bit ends in the middle.

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. PredictNot enough rounds

    Predict where Bit ends up.

  2. ModifyEvery other square

    Now the gems are on every second square. Change the loop body so Bit picks all three and reaches the flag.

  3. WriteGem staircase

    Walk down the staircase, picking the gem on each landing, then finish on the flag. Use a loop (at most 7 lines).

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
Pattern规律A group of steps that repeats and becomes the body of a loop.
Off-by-one error差一错误A bug where a loop runs one time too many or too few.