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.
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
variables.py
from turtle import *
sides = 5
size = 80
angle = 360 / sides
for i inrange(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 inrange(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 inrange(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 = 50forward(size)
size = 100forward(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.
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.
I can store a value in a variable and use its name instead of the number.
I can work out a value with a calculation, such as angle = 360 / sides.
I can change a whole drawing by changing one variable.
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
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.
Read = aloud as 'store'. sides = 5 means 'store 5 in sides', not 'sides equals 5'. This one habit prevents most confusion later.
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.
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.
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
The shape does not close, or the turtle spins in place. Ask: What is stored in angle when sides is 5? Work it out on paper. Fix: angle must be 360 / sides (divide, not multiply).
A NameError says a name is not defined. Ask: Is the name spelt exactly the same everywhere, including capitals? Fix: Variable names must match exactly. Size and size are different names.
A student changes a variable after the loop and expects the shape to change. Ask: Which lines already ran before you changed it? Fix: Lines use the value a name holds when they run. Put the change above the loop.
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
Investigate: A variable is a named value: you can follow how it is used, find where it goes wrong, and change it in one place.
Modify: Changing the program through a variable is safer than changing many numbers: one edit, whole shape updated.
Now you can
Store a value in a variable and use its name instead of the number.
Work out a value with a calculation, such as angle = 360 / sides.