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

Repeat with for · Robot Quest

How do you repeat a command without writing it again and again?

Course contents

Why 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.
  • i is 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 lesson

Watch line 2 light up six times while line 1 counts.

for i in range(6):
    move()
Step by step
  1. L1range(6) prepares the values 0 to 5. First round: i = 0.i = 0
  2. L2The body runs: Bit moves.i = 0
  3. L1Back to the top: i = 1, and the body runs again.i = 1
  4. L2...this repeats until i = 5, the sixth round.i = 5
  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 lesson

Line 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()
Step by step
  1. L2Two moves east (the first loop).
  2. L3The loop is over; turn right once.
  3. 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()
  1. AOn the flag
  2. BOne square before the flag
  3. CIt crashes
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()
  1. AOn the flag
  2. BOn the gem square, gem picked
  3. CError: no gem here
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.

  1. PredictCount the moves

    How far does Bit go?

  2. ModifyLonger corridor

    The corridor is now 9 squares long. Change the loop so Bit reaches the flag. Keep it to 2 lines.

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

TermChineseDefinition
Loop循环A structure that repeats a block of code.
for loopfor 循环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.