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.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
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.
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
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).
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.
Think, pair, share(start) Would you rather give a robot 100 separate instructions, or one instruction to repeat 100 times? What could go wrong with each?
Which is easier to change if the plan changes?
Which is easier to get wrong without noticing?
Is there ever a time copying the lines would be the better choice?
Let students defend both sides. Copying wins when each step is different, when you will only ever do it once, or when a reader needs to see every step. The loop wins when the steps are the same and the count may change. Return to this after the staircase Make: did anyone choose copying, and why?
By the end of this lesson(start)
Here is a shape drawn by 3 lines of Python. By the end of the lesson you will write programs like it.
Run the 36-sided shape from the stretch task on the projector (for i in range(36): forward(20) right(10)) before anything else. Ask: how many lines of code do you think that took?
Play turtle(investigate, 5 min)
One volunteer is the turtle, standing in the middle facing the window.
The class reads the loop aloud, one line at a time. The turtle obeys: 3 steps for forward(100), a quarter turn for right(90).
Now try it with range(3) and right(120). Where does the turtle end up?
Papert designed the turtle so learners could play it. Walking the shape makes two things concrete: only the indented lines repeat, and a closed shape turns all the way round (360). If the turtle turns 60 for the triangle, ask the class to watch which way they end up facing.
Optional: pairs at one computer(modify)
One person types (driver), the other reads the task and checks each run (navigator).
Swap at every new shape.
Pair programming keeps both students thinking and talking, and it helps students who are less confident.
Count the changes(investigate) The loop version draws a 20-sided shape. How many numbers in it change? 2 Only range(20) and the turn (360 / 20 = 18 degrees). The loop is the same 3 lines.
Which way now?(investigate) The turtle starts in the middle, facing right. After the square, which way is it facing? Right Four turns of 90 make 360 degrees, a full turn, so the turtle faces right again. Any shape that closes up turns 360 degrees in total.
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 see
A question to ask
The 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
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.
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.
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.
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 inrange(3):
forward(100)
right(120)
Make
from turtle import *
for i inrange(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
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.
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.