Students write functions that give an answer back with return, see why print is not the same, and combine small functions to build a street.
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 10 min · Make 15 min · Street 5 min
Read the unit primer first. The one idea to be clear on: print shows a value on the screen, but the program cannot use it; return hands the value back to the line that called the function, so it can be used, like forward(metres(5)). Try the bug task yourself: it stops with a TypeError because a function without return gives back None (Python's word for "nothing").
return versus print: both show a number, but only return hands it back. Let the NoneType error happen, then ask what the function gives back.
| What you will see | A question to ask | The fix |
|---|---|---|
| A TypeError mentioning NoneType, often on a forward line. | Does the function return its answer, or only print it? | Replace print(...) inside the function with return ..., then use the value where it is needed. |
| The area is worked out but nothing appears. | Where is the returned value used or shown? | A returned value has to be used. Print it outside the function, print(area(5, 4)). |
| Code after return never runs. | What happens at the moment a function reaches return? | return ends the function. Put any other lines before it. |
One possible solution for each task. Other correct programs are fine.
from turtle import *
scale = 20
def metres(m):
return m * scale
def room(width, depth):
for i in range(2):
forward(metres(width))
right(90)
forward(metres(depth))
right(90)
def area(width, depth):
return width * depth
room(5, 4)
from turtle import *
scale = 20
def metres(m):
return m * scale
def room(width, depth):
for i in range(2):
forward(metres(width))
right(90)
forward(metres(depth))
right(90)
def area(width, depth):
return width * depth
room(5, 4)
print(area(5, 4))
from turtle import *
def house(size):
for i in range(4):
forward(size)
right(90)
left(60)
forward(size)
right(120)
forward(size)
right(120)
forward(size)
right(180)
house(60)
from turtle import *
def house(size):
for i in range(4):
forward(size)
right(90)
left(60)
forward(size)
right(120)
forward(size)
right(120)
forward(size)
right(180)
penup()
goto(-200, 0)
pendown()
for i in range(5):
house(50)
penup()
forward(80)
pendown()
from turtle import *
def house(size):
for i in range(4):
forward(size)
right(90)
left(60)
forward(size)
right(120)
forward(size)
right(120)
forward(size)
right(180)
def move(distance):
penup()
forward(distance)
pendown()
penup()
goto(-220, 0)
pendown()
for i in range(6):
house(70 - i * 10)
move(75)
Stretch: Write a function that returns the perimeter of a room, then print the area and perimeter of three rooms.
Support: Before the Make step, write the one-house function together on the board, then let students add the loop themselves.
def double(n): return n * 2. What does forward(double(30)) do?
double(30) gives back 60, so the turtle moves forward 60 steps.
Next lesson: functions that decide, an if inside a function, called for every item in a list. Then modules keep related functions where other programs can import them. Calculators, maps and games are full of functions that take something in and give an answer back. Class ideas: a class toolbox of unit conversions; a street where every house has a different design.