Students write a function with an if inside that chooses its answer, call it for every size in a list to colour dots, trace its decisions, find a counter reset inside the loop, add an elif and a counting function, and colour score bars.
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
A revisit lesson: nothing in it is new. If and else, lists, for-each and functions that return come together: an if inside a function, with return in each branch, so the function chooses its answer; then a loop over a list calls it for every item. Students who found if or lists shaky get them again in a new place, which is the point.
The planted bug is the classic counting slip: count = 0 inside the loop resets the count every time round. The trace has one edge case on purpose: 50 is not more than 50.
The counter is where students struggle: they put count = 0 wherever it fits. Ask them to trace the loop with a table on paper (s, count) before changing a line.
| What you will see | A question to ask | The fix |
|---|---|---|
| The count is always 0 or 1. | Where does count start at 0, inside the loop or before it? | Set count = 0 once, before the loop. |
| TypeError: a colour is None. | Does every branch of the function return a value? | Each if, elif and else needs its own return (or print was used instead of return). |
| Every dot is the same colour. | Is the function called with each s, or with the same number every time? | Call size_colour(s) inside the loop. |
One possible solution for each task. Other correct programs are fine.
from turtle import *
speed(0)
def size_colour(size):
if size > 50:
return "red"
elif size > 30:
return "orange"
else:
return "blue"
sizes = [20, 60, 35, 80, 50]
penup()
goto(-160, 0)
for s in sizes:
dot(s, size_colour(s))
forward(80)
from turtle import *
speed(0)
def size_colour(size):
if size > 50:
return "red"
else:
return "blue"
def count_big(sizes):
count = 0
for s in sizes:
if s > 50:
count = count + 1
return count
sizes = [20, 60, 35, 80, 50]
penup()
goto(-160, 0)
for s in sizes:
dot(s, size_colour(s))
forward(80)
print(count_big(sizes))
from turtle import *
speed(0)
scores = [45, 80, 62, 30, 95]
def score_colour(score):
if score >= 70:
return "green"
elif score >= 50:
return "orange"
else:
return "red"
def bar(height, colour):
fillcolor(colour)
begin_fill()
for i in range(2):
forward(30)
left(90)
forward(height)
left(90)
end_fill()
penup()
goto(-150, -80)
pendown()
for s in scores:
bar(s * 2, score_colour(s))
penup()
forward(50)
pendown()
Stretch: Write is_even(n) that returns True or False, and use it to draw only the even-numbered dots. Return a size as well as a colour from two functions.
Support: Give the counting function with count = 0 missing; students decide where it goes.
def grade(score): if score >= 50: return "pass" else: return "try again". What is grade(50)?
pass: 50 is greater than or equal to 50.
Next lesson: modules, a place to keep useful functions such as size_colour. The same pattern (a function that decides, called for each item) appears in Catch the turtle's hit test and in the console bridge's menus.