← Python Foundations with Turtle

Repeat with a loop

Predict, run and change short programs that repeat lines with a loop.

Loops are how a program draws 100 tiles, sends 100 emails or reads 100 sensors without 100 lines of code.

Last lesson you drew digits by writing every command one after another. A square written that way takes eight lines. There is a shorter way.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

square.py
from turtle import *

for i in range(4):
    forward(100)
    right(90)

The line for i in range(4): is new. Read the code. What will the turtle draw?

Reveal what happens
Predict · Sound on for the voiceover
What will the turtle draw?

RunRun it and compare the result with your prediction.

Run

Run the program and compare it with your prediction. Which lines ran four times?

Here is what each line does.

Line What it does Why
for i in range(4): Starts a loop that runs four times range(4) counts 0, 1, 2, 3: one run each
forward(100) Draws a side It is indented, so it is part of the loop
right(90) Turns a quarter turn Also indented, so it repeats too

Without the loop, the same square is eight lines. The loop writes the pattern once, and the count is a number you can change.

square.py
from turtle import *

for i in range(4):
    forward(100)
    right(90)
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

Compare the two programs. They draw the same square.

square.py
# Without a loop
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)

# With a loop
for i in range(4):
    forward(100)
    right(90)
Investigate

How much code does each way need as the shape gets more sides?

Indented lines belong to the loop. Find the line that should repeat but doesn't.

In for i in range(4):, the name i is a counter. Follow it round the loop.

A square drawn one command at a time uses 8 lines of drawing code. A 20-sided shape drawn the same way would use how many lines?

This was meant to draw a square. Instead the turtle draws one long line and turns once at the very end. Click the wrong line.

for i in range(4): makes a counter called i. It starts at 0 and goes up by 1 each time round the loop. Fill in the table for the square.

passiturned so far
1090
2
3
4

ModifyChange the code on purpose and check the result.

Modify

Change the loop so it draws a triangle instead of a square. Run after each change.

Try it yourselfYour edits are saved in this browser.
Need a hint?

A square turns the turtle 90 degrees four times. How many times does a triangle turn, and what do all the turns add up to?

Another hint

Two numbers change: how many times the loop repeats, and how far the turtle turns each time.

Show what the triangle should look like
The expected result: a triangle · Sound on for the voiceover

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

A loop works for any pattern, not only shapes. Find the part that repeats, write it once, and say how many times.

Draw a staircase with 5 steps, like the picture. When it works, change one number to make it 10 steps. Then try a pattern of your own: a zigzag, a row of battlements or a dashed line. Find the part that repeats first.

Next lesson adds variables. Then you put loops and variables to work on a whole program: a street of houses.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Trace one step with your finger. Which moves and turns make one step?

Another hint

One step is four lines: up, turn, across, turn back. Put those under a for line.

Show what the staircase should look like
The expected result: a staircase with 5 steps · Sound on for the voiceover

Draw a staircase with 5 steps using a loop.

    Run your program. This ticks when your code does all of the above.

    For teachers

    Students see that the square they drew with eight lines can be written with three, using a loop, and find out how to change the count and the turn. About 35 minutes.

    Open the teacher guide: answers, solutions, slides and a worksheet.

    Students will be able to

    Before class

    Open the lesson, run the first program and draw the triangle from the Modify step yourself. It takes about five minutes and you will meet the same surprise students do: the turn is 120 degrees, not 60.

    Running the lesson

    1. Predict: show the loop and ask what it draws. Take two or three answers before anyone runs it; each wrong option is a different idea of what a loop repeats (only the first line, only once, nothing because i is unused).
    2. Run: run it live on the slide, then students run it on their own screens and compare with their prediction. Ask: which lines ran four times, and how do you know? Then show the eight-line square beside the loop.
    3. Investigate: the key idea is that a loop does the same work as copied lines, but stays the same size. Ask a student to say why the loop version is easier to change. The bug task (right(90) not indented) comes after students have seen the loop work. The trace task traces the counter i (0, 1, 2, 3); say that it counts from 0, because later lessons use it.
    4. Modify: work the triangle live on the slide first if the room is unsure, then let students go on alone. Let them get it wrong first. If they try 60 degrees, ask them to walk the shape themselves: how far do you turn at each corner?
    5. Make: the staircase reverses the lesson: students find the part that repeats in a picture and write it once. Early finishers can try the house (a square, then a roof turning left 60 and right 120).

    The hard part (on purpose)

    Working out the turn angle for a shape. Ask how far the turtle turns in total and how many turns there are, instead of giving the number.

    When students get stuck

    Stretch and support

    Stretch: Draw a shape with 50 sides and turn 360 / 50 degrees. What does it start to look like, and why?

    Support: Give the loop version and ask the student to change only one number at a time, running after each change.

    Where it leads

    Loops come back in every lesson, and in tessellations, logo design and generative art. Next lesson stores numbers in variables, so the turn can be worked out instead of guessed. Then A street of houses reads a whole program that uses both. Class ideas: design a floor tile and repeat it; recreate a simple logo from code.

    What each step is for

    Now you can
    Next lesson3. Name a valueGive a number a name, then let the program work out the rest.