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.
Make your prediction first. Help opens once you've locked it in.
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?
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.
from turtle import *
sides = 5
size = 80
angle = 360 / sides
for i in range(sides):
forward(size)
right(angle)
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.
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.
Something small in here stops the shape from closing.
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.
This should draw a hexagon, but the turtle draws one long line and turns once at the end. Click the wrong line.
Change one number at the top to draw a hexagon, then a 12-sided shape. Run it each time.
Before a hint: what kind of stuck is this? Try a strategy first
You should only need to change one number. Which variable controls the number of sides?
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.
Before a hint: what kind of stuck is this? Try a strategy first
Put three names at the top: sides, size and colour. Then use each name below.
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.
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.
Lesson done. You've finished every step you need.
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.
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.
Seeing that a name holds a value the program can reuse. Ask them to say, for each line, which stored value it uses.
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.
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.