Students import functions from a module (a separate file of related functions), see why copied code costs more to fix, then build a module of their own.
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 15 min
A module is a Python file of functions that other programs can use. from shapes import square means "find the file shapes.py and bring in its square function". In the playground, + module makes a new file tab. Before class, do the Modify step yourself: move the three house functions into a module and import house.
Seeing why a module beats copying, and that a def with the same name replaces an import. Ask how many places they'd have to fix if the code were copied.
| What you will see | A question to ask | The fix |
|---|---|---|
| A ModuleNotFoundError or ImportError. | Is the module's file name exactly the name after from, without .py? | The tab name and the import must match exactly, in lowercase. |
| The drawing happens twice, or as soon as the module is imported. | Are there calls (not just defs) inside the module? | Keep only def blocks in the module. Calls go in main.py. |
| An imported function behaves differently from what the module says. | Is there a def with the same name in main.py? | A def in main.py with the same name replaces the imported one. Rename or delete one of them. |
One possible solution for each task. Other correct programs are fine.
from turtle import *
from house_parts import house
house(60)
from turtle import *
def wall(size):
for i in range(4):
forward(size)
right(360 / 4)
def roof(size):
left(60)
forward(size)
right(120)
forward(size)
right(120)
forward(size)
right(180)
def house(size):
wall(size)
roof(size)
from turtle import *
from garden import tree, flower, fence
speed(0)
penup()
goto(-180, -80)
pendown()
fence(18)
penup()
goto(-120, -80)
pendown()
tree(60)
penup()
goto(60, -80)
pendown()
flower(25)
from turtle import *
def tree(height):
"""A trunk and a round top."""
setheading(90)
pencolor("brown")
forward(height)
pencolor("green")
dot(height)
setheading(0)
def flower(size):
"""Five petals round a centre."""
pencolor("purple")
for i in range(5):
circle(size / 2)
left(72)
dot(size / 2, "gold")
def fence(posts):
"""A row of fence posts, 20 apart."""
setheading(0)
pencolor("grey")
for i in range(posts):
left(90)
forward(30)
backward(30)
right(90)
forward(20)
from turtle import *
from shapes import square, triangle
for i in range(5):
square(60 + i * 10)
pencolor("red")
triangle(60 + i * 10)
pencolor("black")
penup()
forward(110)
pendown()
Stretch: Write a module with a docstring for each function (a first line in triple quotes). The docstring becomes the hint on the function's card in the Reference panel.
Support: Do the Modify step together on the projector, then let students make a two-function module before attempting three.
Two programs each have their own copy of a draw_tree function with a bug. Why would putting draw_tree in a module be better?
Both programs would import the one function, so fixing it once in the module fixes it for both.
Next unit makes programs react to clicks and keys. Every real Python program imports modules others wrote (random, math, and libraries like Gradio). Class ideas: each student builds a module for a theme and a classmate imports it; a class town built from everyone's modules.