← Python Foundations with Turtle

Name a value

Give a number a name, then let the program work out the rest.

Games keep score, maps keep scale and apps keep your settings. All of them store a value under a name and calculate with it.

In the last lesson the numbers were typed straight into each command. Here you give a number a name, so you can change it in one place and let the program work out the rest.

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

variables.py
from turtle import *

sides = 5
size = 80
angle = 360 / sides

for i in range(sides):
    forward(size)
    right(angle)

angle = 360 / sides stores the result of a calculation. What will the turtle draw? How many sides, and why does right(angle) turn by that amount?

How many sides will the shape have?

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
sides = 5 Stores 5 under the name sides Read = as "store": put 5 in a box called sides
size = 80 Stores 80 under the name size Change it here and every size below changes
angle = 360 / sides Works out the turn and stores it A full turn is 360 degrees, split between the corners
range(sides) Repeats sides times The name stands in for 5
right(angle) Turns by whatever angle holds 72 here, but you never typed 72

Change sides and the turn updates by itself.

variables.py
from turtle import *

sides = 5
size = 80
angle = 360 / sides

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

Investigate

print(angle) shows a value under the drawing. Copy it whenever you want to see what a name holds; the console lessons explain how it works. Change sides and watch what angle becomes, then work through the checks.

variables.py
from turtle import *

sides = 5
size = 80
angle = 360 / sides
print(angle)

for i in range(sides):
    forward(size)
    right(angle)

Work out angle yourself before you check.

Here is a version where something is wrong.

A name holds one value at a time. Lines that ran earlier keep what they used.

size = 50
forward(size)
size = 100
forward(size)

You have met this one before, now with names in it.

If sides = 6, what is angle?

This program should draw a 5-sided shape, but it does not close. Click the wrong line.

The program stores size = 50 and draws a line. Later it stores size = 100 and draws another line. How long was the first line?

This should draw a hexagon, but the turtle draws one long line and turns once at the end. Click the wrong line.

ModifyChange the code on purpose and check the result.

Modify

Change one number at the top to draw a hexagon, then a 12-sided shape. Run it each time.

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

You should only need to change one number. Which variable controls the number of sides?

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

Make

Make your own shape generator. Put three names at the top (sides, size and colour) and use all three below. Then change each one and watch the shape change.

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

Put three names at the top: sides, size and colour. Then use each name below.

Another hint

A colour is text, so it goes in quotes: colour = "purple". Use it with pencolor(colour).

Use a variable for the pen colour and draw a shape with a loop.

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

    ModifyChange the code on purpose and check the result.

    Resize a room plan

    Plans and maps store a scale: how many pixels are one metre. This program draws a 6 m by 4 m room. Change the room to 8 m by 5 m, then change scale and notice the whole plan resizes. Then add a second room beside it.

    Try it yourselfYour edits are saved in this browser.
    For teachers

    Students give numbers names (variables) and let the program calculate the turn, so one change at the top updates the whole shape. About 40 minutes.

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

    Students will be able to

    Before class

    Run the first program, then make the shape generator from the Make step. Notice how little you change to get a different shape: that is the point of the lesson.

    Running the lesson

    1. Predict: ask how many sides the shape will have and why the turtle turns 72 degrees. The answer is in the line angle = 360 / sides.
    2. Read = aloud as 'store'. sides = 5 means 'store 5 in sides', not 'sides equals 5'. This one habit prevents most confusion later.
    3. Investigate: students work the angle for 6 sides, find the multiply-for-divide bug, decide what a later store does to a line already drawn (it does nothing to it), and find the unindented turn in a loop they met last lesson.
    4. Modify: students should only ever change the top numbers. If someone edits inside the loop, ask them to find the one variable that does the job.
    5. Make: three names and a loop. Let students choose their own values; the check only needs a variable-coloured pen and a loop.

    The hard part (on purpose)

    Seeing that a name holds a value the program can reuse. Ask them to say, for each line, which stored value it uses.

    When students get stuck

    Stretch and support

    Stretch: Change sides to 3, 4, 5 and up to 20, and record which values still look like the shape and which look like a circle.

    Support: Give students a table with two columns (sides, angle) and ask them to fill in angle for 3, 4, 5 and 6 before running anything.

    Where it leads

    Next lesson uses the loop counter, so a value can change each pass without being typed. After that, a value that grows. Scores, game settings and the scale on maps and plans all use variables. Class ideas: a spiral gallery; scale drawings of the classroom.

    What each step is for

    Now you can
    Next lesson4. Use the counterThe loop counter i changes every pass, so a line can grow without you typing each number.