Students give a function inputs (parameters), so one function can draw many sizes and shapes, then build a pattern by calling one function from another.
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.
Predict and Run 10 min · Investigate 15 min · Modify 15 min · Make 10 min · My tools 5 min
Run the three nested squares and try adding a colour parameter to polygon. Two words to have straight: a parameter is the name in the def (size), an argument is the value you pass when calling (50). The argument fills the parameter, in order.
Arguments fill parameters in order. Read the call and the def aloud side by side and match each value to its name.
| What you will see | A question to ask | The fix |
|---|---|---|
| A TypeError: polygon() missing 1 required positional argument. | How many parameters does the def have, and how many values does this call pass? | Every call must pass one value for each parameter, in the same order. |
| The shape has far too many sides or does not close. | Which value went into sides, and which into size? | Arguments match parameters by position. polygon(6, 80), not polygon(80, 6). |
| The pattern draws one shape on top of itself. | Does the turtle turn between the copies? | Add right(360 / copies) inside the loop, after drawing each polygon. |
One possible solution for each task. Other correct programs are fine.
from turtle import *
def polygon(sides, size):
angle = 360 / sides
for i in range(sides):
forward(size)
right(angle)
polygon(3, 80)
polygon(4, 80)
polygon(6, 80)
polygon(8, 50)
from turtle import *
def polygon(sides, size, colour):
pencolor(colour)
angle = 360 / sides
for i in range(sides):
forward(size)
right(angle)
polygon(3, 80, "red")
polygon(4, 80, "blue")
polygon(6, 80, "green")
polygon(8, 50, "purple")
from turtle import *
def polygon(sides, size):
angle = 360 / sides
for i in range(sides):
forward(size)
right(angle)
def pattern(sides, copies):
for i in range(copies):
polygon(sides, 60)
right(360 / copies)
speed(0)
pattern(5, 12)
Open task: any program that does what the step asks.
from turtle import *
scale = 15 # pixels per metre
def room(width, depth):
for i in range(2):
forward(width * scale)
right(90)
forward(depth * scale)
right(90)
def move_to(x, y):
penup()
goto(x * scale, y * scale)
pendown()
move_to(-8, 4)
room(5, 4) # bedroom
move_to(1, 4)
room(4, 4) # kitchen, moved to the right
move_to(-3, 4)
room(4, 2) # bathroom
move_to(-8, 0)
room(9, 3) # living room
Stretch: Add a third parameter to pattern for the size, and draw a gallery of three different patterns side by side.
Support: Give students a table of calls (square(50), square(100)) and ask them to sketch what each draws before running.
def polygon(sides, size): ... What does polygon(5, 40) draw?
A pentagon (5 sides) with each side 40 steps long, turning 72 degrees at each corner.
Next lesson functions give answers back with return. Parameters are how every app passes information around. Class ideas: a pattern gallery where each student chooses sides and copies; compare function designs and talk about what makes one easy to reuse.