Patterns in a loop · Robot Quest
How do you find the part of a task that repeats?
Course contentsWhy 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 againPut 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
A row of gems
Run it in the lessonThe pattern is: move, pick. The last move() is outside the loop.
for i in range(5):
move()
pick()
move()- L2Round 1: move onto the first gem...
i = 0 - L3...and pick it. One round = one gem.
i = 0 - L2Round 2 starts in the same situation: facing east, a gem ahead.
i = 1 - L4After 5 rounds all gems are collected; one last move reaches the flag.
Climbing stairs
Run it in the lessonFour lines make one step. Three rounds climb three steps.
for i in range(3):
move()
turn_left()
move()
turn_right()- L2Forward one square.
- L3Turn to face up.
- L4Up one square.
- L5Turn back to face forward: the same situation as at the start of the round.
- •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()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()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.
- PredictNot enough rounds
Predict where Bit ends up.
- ModifyEvery other square
Now the gems are on every second square. Change the loop body so Bit picks all three and reaches the flag.
- 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
| Term | Chinese | Definition |
|---|---|---|
| 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. |