← Python Foundations with Turtle

A value that grows

A variable can change itself, so a value grows each time round a loop.

Scores go up, timers count down and bank balances change. All of them are a variable that works out its next value from its last one.

Last lesson the counter i made the lines grow. A variable can do the same job on its own.

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

grow.py
from turtle import *

size = 0

for i in range(30):
    forward(size)
    right(90)
    size = size + 5

size = size + 5 is new. Read the code. Will the spiral look different from last lesson's?

Last lesson the spiral used the counter. This one uses size = size + 5. What will it draw?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction.

Here is what each line does.

Line What it does Why
size = 0 Starts size at 0 It is before the loop, so it only happens once
forward(size) Draws a line size long Uses whatever size holds right now
size = size + 5 Works out size + 5, then stores the answer back in size Read the right side first, then store. It is inside the loop, so it happens every pass

The picture is the same as last lesson's. There the counter i did the growing; here size does it.

grow.py
from turtle import *

size = 0

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

Investigate

Follow the value as it changes.

grow.py
from turtle import *

size = 0

for i in range(30):
    forward(size)
    right(90)
    size = size + 5

A variable can change itself. The line size = size + 5 works out size + 5, then stores the answer back in size.

Follow size round this loop, which adds to it every pass.

size = 10

for i in range(4):
    forward(size)
    right(90)
    size = size + 10

An update has to be inside the loop to happen every pass.

Compare this lesson's way with last lesson's counter.

A program has size = 20. Then it runs the line size = size + 5. What is stored in size afterwards?

Fill in the size that forward(size) uses on each pass.

passsize used by forward(size)
110
2
3
4

This should draw a growing spiral, but it draws one small square over and over. Click the wrong line.

Last lesson's lines were i * 5. This lesson's size starts at 0. Which update inside the loop gives the same lengths?

ModifyChange the code on purpose and check the result.

Modify

Change the amount added each pass so the spiral grows faster. Run it after each change.

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

Only the number being added needs to change.

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

Make

Make the spiral grow and get thicker on every pass. The thickness should be a variable that updates itself, like size does.

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

Give the thickness its own variable, like size, and set it before the loop.

Another hint

Use pensize(width) inside the loop, and add to width at the end of each pass.

Make the pen thicker every pass using a variable.

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

    For teachers

    Students meet a variable that changes itself, size = size + 5, and use it to grow a spiral without the loop counter. About 35 minutes.

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

    Students will be able to

    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

    1. 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.
    2. 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'.
    3. 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.
    4. Modify: one number changes. Let students predict before they run.
    5. Make: a second variable that updates itself. Students who finish can try changing the amount added to the thickness.

    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

    Stretch and support

    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.

    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.

    What each step is for

    Now you can
    Next lesson6. A street of housesRead a whole program that uses loops and variables together, then change it and add to it.