← Teacher guide

Functions that decide

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.

Python functions and events · Functions · about 50 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 15 min · Modify 10 min · Make 15 min

Before class

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.

Running the lesson

  1. Predict: ask students to read the function aloud as a sentence: if the size is over 50, give back red, otherwise blue.
  2. Run: change 50 to 30 and ask which dots changed and why.
  3. Investigate: do the trace before running anything; argue about 50.
  4. Modify: for count_big, ask "when should count start at 0: once, or every time round?"
  5. Make: ask students to draw one bar first, then wrap it in a loop.

The hard part (on purpose)

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.

When students get stuck

What you will seeA question to askThe 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.

Answers to the checks

  1. Predict: What two words are printed?
    red, then blue. 80 is more than 50, so the if returns red; 30 is not, so the else returns blue.
  2. Trace the decisions: Fill in what size_colour gives back for each size in the list.
    blue, red, blue
    50 is not more than 50, so it goes to the else: blue. > means more than, not equal to.
  3. Why a function?: Why put the if inside a function, and not straight in the loop?
    The decision gets a name and can be used anywhere: in this loop, in another loop, in a print
    Named once, size_colour can be reused and changed in one place. Change the rule inside it, and every place that uses it follows.
  4. Spot the bug: There are two sizes over 50, but this prints 1. There is no error. Click the line that is wrong.
    line 3
    count = 0 is inside the loop, so it goes back to 0 for every size: only the last count survives. Set it to 0 once, before the loop.

Solutions

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

Modify › Three colours

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)

Modify › Count the big ones

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))

score bars

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 and support

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.

Exit ticket

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.

What each step is for

Where it leads

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.