← Teacher guide

Repeat with a loop

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.

Python Foundations with Turtle · Commands, loops and values · about 35 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 10 min · Modify 8 min · Make 7 min

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

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

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

What you will seeA question to askThe fix
The triangle has a gap, or the shape looks like a star.How many times does the loop repeat, and how far does the turtle turn each time?A shape with 3 sides repeats 3 times and turns 120 degrees. All the turns add up to 360.
Only the first line repeats, or the program stops with an indentation error.Which lines are pushed in under the for line?The lines that repeat must be indented (four spaces) under the for line.
A student types forward(100) and right(90) many times and sees no reason to change.How long would that be for a 50-sided shape?Ask them to draw a 50-sided shape both ways. The loop stays 3 lines; the copied version needs 100.

Answers to the checks

  1. Predict: What will the turtle draw?
    A square. The two indented lines run 4 times: a side of 100, then a quarter turn, four times over. The turtle finishes where it started, facing right.
  2. Count the lines: 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?
    40
    Each side needs 2 lines (forward and a turn), so 20 sides need 40.
  3. Spot the bug: 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.
    line 5
    right(90) is not indented, so it is not part of the loop. The loop draws four sides in a straight line, and the turn runs once afterwards. Indent it under the for line so both lines repeat.
  4. Follow the counter: 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.
    1, 180, 2, 270, 3, 360
    i counts 0, 1, 2, 3: four passes, starting from 0. This square never uses i, but it is there, and later programs will use it. After four turns of 90 the turtle has turned 360 degrees, a full turn.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

from turtle import *

for i in range(3):
    forward(100)
    right(120)

Make

from turtle import *

for i in range(5):
    left(90)
    forward(20)
    right(90)
    forward(20)

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.

Exit ticket

A turtle draws a shape with 5 equal sides using a loop. How many times does the loop repeat, and how far does the turtle turn each time?

5 times, turning 72 degrees each time (360 divided by 5).

What each step is for

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.