Repeat with for · Robot Quest
How do you repeat a command without writing it again and again?
Course contentsWhy you need it
Walking 12 squares with 12 move() lines works, but it is slow to write, easy to miscount, and painful to change. Programmers almost never copy the same line many times. Instead they write a loop: "do this, N times".
The intuition
A coach says "Run 5 laps" rather than "run a lap, run a lap, run a lap, run a lap, run a lap".
In Python:
for i in range(5):
move()range(5)means "5 times".- The colon
:at the end says "the part to repeat comes next". - The repeated lines are indented by 4 spaces. That indented block is the loop body.
iis a counter Python updates for you (0, 1, 2, 3, 4). You don't have to use it.
A line that is not indented runs once, after the loop has finished.
Definition
for loop
A for loop repeats its indented body once for each value in a sequence; for i in range(n) repeats it n times.
Indentation
In Python, indentation (4 spaces) shows which lines belong to a block, such as the body of a loop.
Press Tab in the editor to insert 4 spaces.
Examples
Six steps, two lines
Run it in the lessonWatch line 2 light up six times while line 1 counts.
for i in range(6):
move()- L1range(6) prepares the values 0 to 5. First round: i = 0.
i = 0 - L2The body runs: Bit moves.
i = 0 - L1Back to the top: i = 1, and the body runs again.
i = 1 - L2...this repeats until i = 5, the sixth round.
i = 5 - •No values left in range(6), so the loop ends. Bit is on the flag.
What runs after the loop
Run it in the lessonLine 3 is not indented, so it runs once, after all the moves.
for i in range(2):
move()
turn_right()
for i in range(3):
move()- L2Two moves east (the first loop).
- L3The loop is over; turn right once.
- L5A second loop: three moves down to the flag.
Common mistakes
Missing colon or indentation
for i in range(3) without : is a syntax error. A body that isn't indented is an error too: Python can't tell what to repeat.
Indenting a line that should run once
If turn_right() is indented, it becomes part of the loop and runs every round.
Quick quiz
1. Where does Bit end up?
for i in range(4):
move()Show the answer
Answer: A
range(4) gives 4 rounds, so 4 moves: exactly to the flag.
2. Where does Bit end up?
for i in range(3):
move()
pick()Show the answer
Answer: B
pick() runs once after three moves. Bit is on the gem, but nothing moves it to the flag.
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.
- PredictCount the moves
How far does Bit go?
- ModifyLonger corridor
The corridor is now 9 squares long. Change the loop so Bit reaches the flag. Keep it to 2 lines.
- WriteLong L
Seven squares east, then three squares up. Use loops: at most 5 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 |
|---|---|---|
| Loop | 循环 | A structure that repeats a block of code. |
| for loop | for 循环 | A loop that runs its body once for each value in a sequence, such as range(n). |
| Indentation | 缩进 | Spaces at the start of a line that show which block the line belongs to. |
| Loop body | 循环体 | The indented lines that a loop repeats. |