Students meet a variable that changes itself, size = size + 5, and use it to grow a spiral without the loop counter.
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 say what size = size + 5 does.
I can use an update inside a loop to make a value grow.
I can find an update that is in the wrong place.
Timing
Predict and Run 10 min · Investigate 12 min · Modify 5 min · Make 8 min
Before class
Run the spiral, then make the thickening spiral from the Make step. The key line is size = size + 5, which looks wrong to anyone used to maths: in Python it means "work out size + 5, then store it back in size". Be ready to say that out loud.
Running the lesson
Predict: show the spiral and ask whether it looks different from last lesson's. The wrong options include an error, because many students think size cannot equal size + 5. The counter spiral and the update spiral look the same: ask why before running.
Say it aloud: read size = size + 5 right to left. Work out size + 5, then store the answer back in size. Say 'store' instead of 'equals'.
Investigate: settle what is stored, trace the value over four passes, then show the update outside the loop and ask what happens. The last question links back to the counter: two ways to get the same lengths.
Modify: one number changes. Let students predict before they run.
Make: a second variable that updates itself. Students who finish can try changing the amount added to the thickness.
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.
By the end of this lesson(start)
Here is the finished program running. By the end of the lesson you will write one like it.
Run the finished example on the projector before anything else.
Trace size(investigate, 5 min)
The class calls out size after each pass while one student updates a number on the board.
Make the idea physical before it goes back to the screen.
The hard part (on purpose)
Reading size = size + 5 as an equation. Have them trace the first three values on paper before running it.
When students get stuck
What you will see
A question to ask
The fix
The spiral is a square, with every line the same length.
Does anything change size while the loop runs?
Add size = size + 5 as the last line inside the loop (indented).
One small square is drawn over and over.
Is the update line indented under the for line?
The update must be inside the loop to run on every pass.
A student thinks size = size + 5 is an error.
Which side does Python work out first?
Work out the right side, then store the answer in the name on the left.
Answers to the checks
Predict: Last lesson the spiral used the counter. This one uses size = size + 5. What will it draw? The same spiral. size starts at 0 and grows by 5 on every pass: 0, 5, 10, 15. That is exactly what i * 5 gave last lesson.
Store it back: A program has size = 20. Then it runs the line size = size + 5. What is stored in size afterwards? 25 Read the line from the right. Python works out size + 5, which is 25, then stores 25 back in size. The = means store, not 'is equal to'.
Watch it grow: Fill in the size that forward(size) uses on each pass. 20, 30, 40 The last line of each pass stores size + 10 back in size, so the next pass starts with a bigger size. This is how a spiral grows.
Spot the bug: This should draw a growing spiral, but it draws one small square over and over. Click the wrong line. line 8 size = size + 5 is not indented, so it runs once, after the loop. Inside the loop it runs on every pass. Indent it under the for line.
Same lengths, two ways: Last lesson's lines were i * 5. This lesson's size starts at 0. Which update inside the loop gives the same lengths? size = size + 5 Adding 5 each pass gives 0, 5, 10, 15, the same as i * 5. size * 5 stays at 0, size = i gives 0, 1, 2, and size = 5 is always 5.
Solutions
One possible solution for each task. Other correct programs are fine.
Modify
from turtle import *
size = 0for i inrange(30):
forward(size)
right(90)
size = size + 10
Stretch: Make the spiral shrink instead, by subtracting each pass. What happens when size goes below 0?
Support: Give the trace table with the first two rows filled in and ask the student to continue it.
Exit ticket
What does size = size + 5 do if size is 20?
It works out 20 + 5 = 25 and stores 25 back in size.
What each step is for
Investigate: A variable can change itself: the right side is worked out first with the old value, and the answer is stored back.
Modify: Changing the amount added changes how fast the value grows, with one edit.
Where it leads
Next, a street of houses reads a whole program that uses loops, names, the counter and updates together. Scores, timers and bank balances all update this way. Class ideas: a growing-colour spiral; a countdown.