← Teacher guide

Name a value

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

Python Foundations with Turtle · Commands, loops and values · about 40 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.

Students will be able to

Timing

Predict and Run 10 min · Investigate 12 min · Modify 6 min · Make 8 min · Room plan 4 min

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.

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.

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

What you will seeA question to askThe fix
The shape does not close, or the turtle spins in place.What is stored in angle when sides is 5? Work it out on paper.angle must be 360 / sides (divide, not multiply).
A NameError says a name is not defined.Is the name spelt exactly the same everywhere, including capitals?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.Which lines already ran before you changed it?Lines use the value a name holds when they run. Put the change above the loop.

Answers to the checks

  1. Predict: How many sides will the shape have?
    Five sides. angle = 360 / 5 = 72, so the turtle turns 72 degrees at each corner and the shape closes.
  2. Calculate the angle: If sides = 6, what is angle?
    60
    360 / 6 = 60, so the turtle turns 60 degrees at each corner.
  3. Spot the bug: This program should draw a 5-sided shape, but it does not close. Click the wrong line.
    line 5
    angle = 360 * sides is too big. To split one full turn between the corners it should be 360 / sides.
  4. A name keeps its value: The program stores size = 50 and draws a line. Later it stores size = 100 and draws another line. How long was the first line?
    50
    A line uses the value size holds at that moment. Storing a new value changes the lines drawn after it, not the ones already drawn.
  5. Spot the bug in the loop: This should draw a hexagon, but the turtle draws one long line and turns once at the end. Click the wrong line.
    line 9
    right(angle) is not indented, so it is not part of the loop. Indent it under the for line so both lines repeat.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

from turtle import *

sides = 12
size = 40
angle = 360 / sides

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

Make

from turtle import *

sides = 6
size = 80
angle = 360 / sides
colour = "purple"

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

Resize a room plan

from turtle import *

scale = 20   # pixels per metre
width = 8    # metres
depth = 5

for i in range(2):
    forward(width * scale)
    right(90)
    forward(depth * scale)
    right(90)

# A second room beside the first: 4 m by 5 m.
forward(width * scale)
for i in range(2):
    forward(4 * scale)
    right(90)
    forward(depth * scale)
    right(90)

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.

Exit ticket

A program has sides = 8 and angle = 360 / sides. How far does the turtle turn at each corner?

45 degrees (360 divided by 8).

What each step is for

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.