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.
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
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 inrange(4):
forward(100)
right(90)
The line for i in range(4): is new. Read the code. What will the turtle draw?
Reveal what happensPredict · Sound on for the voiceoverVoice:
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 inrange(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 loopforward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
# With a loopfor i inrange(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.
pass
i
turned so far
1
0
90
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 likeThe expected result: a triangle · Sound on for the voiceoverVoice:
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 likeThe expected result: a staircase with 5 steps · Sound on for the voiceoverVoice:
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.
I can use a loop to repeat lines instead of copying them.
I can say which lines belong to a loop.
I can work out the turn for a shape with any number of sides.
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
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).
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.
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.
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?
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
The triangle has a gap, or the shape looks like a star. Ask: How many times does the loop repeat, and how far does the turtle turn each time? Fix: 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. Ask: Which lines are pushed in under the for line? Fix: 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. Ask: How long would that be for a 50-sided shape? Fix: Ask them to draw a 50-sided shape both ways. The loop stays 3 lines; the copied version needs 100.
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
Investigate: Repetition has a cost: without a loop the code grows with every side, and a loop does the same work with a number you can change.
Modify: Change a working program one small step at a time and check each result; the turn angle comes from the shape, not from guessing.
Now you can
Use a loop to repeat lines instead of copying them.
Say which lines belong to a loop.
Work out the turn for a shape with any number of sides.