← Python functions and events

Read a program

Find the part of a longer program that does one job, then change only that part.

Programmers read far more code than they write. The first skill is finding the part that does the job you want to change.

This program is longer than any you have met, but it uses only things you know. The skill now is reading: finding which lines do which job.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

garden.py
from turtle import *

speed(0)
colours = ["red", "orange", "pink", "purple"]
stem = 50
head = 10

penup()
goto(-150, -80)
pendown()

for i in range(len(colours)):
    # stem
    if i % 2 == 0:
        pencolor("green")
    else:
        pencolor("darkgreen")
    pensize(3)
    left(90)
    forward(stem + i * 10)

    # flower head
    fillcolor(colours[i])
    begin_fill()
    circle(head)
    end_fill()
    head = head + 4

    # back down, then along to the next flower
    penup()
    backward(stem + i * 10)
    right(90)
    forward(70)
    pendown()

Read the code, one comment at a time. How tall is the third stem?

stem is 50. How tall is the third stem (when i is 2)?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction. Which part is the turtle running right now?

garden.py
from turtle import *

speed(0)
colours = ["red", "orange", "pink", "purple"]
stem = 50
head = 10

penup()
goto(-150, -80)
pendown()

for i in range(len(colours)):
    # stem
    if i % 2 == 0:
        pencolor("green")
    else:
        pencolor("darkgreen")
    pensize(3)
    left(90)
    forward(stem + i * 10)

    # flower head
    fillcolor(colours[i])
    begin_fill()
    circle(head)
    end_fill()
    head = head + 4

    # back down, then along to the next flower
    penup()
    backward(stem + i * 10)
    right(90)
    forward(70)
    pendown()
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

garden.py
from turtle import *

speed(0)
colours = ["red", "orange", "pink", "purple"]
stem = 50
head = 10

penup()
goto(-150, -80)
pendown()

for i in range(len(colours)):
    # stem
    if i % 2 == 0:
        pencolor("green")
    else:
        pencolor("darkgreen")
    pensize(3)
    left(90)
    forward(stem + i * 10)

    # flower head
    fillcolor(colours[i])
    begin_fill()
    circle(head)
    end_fill()
    head = head + 4

    # back down, then along to the next flower
    penup()
    backward(stem + i * 10)
    right(90)
    forward(70)
    pendown()

Match each part with its job.

The update is there, but something undoes it.

Match each part of the program with its job.

if i % 2 == 0: ... else: ...
forward(stem + i * 10)
head = head + 4
colours[i]

The flower heads should grow, but all four are the same size. Click the line that stops them growing.

ModifyChange the code on purpose and check the result.

Modify

Add two more colours. Everything else should keep working.

Need a hint?

Which line holds the list of colours? Add to it.

Another hint

Six flowers are wider than four. Move the start further left with goto.

Find the part that makes the stems different, and change only that.

You have found parts by their jobs. In Course B, you will give those parts names, so you can reuse them.

Need a hint?

Which part of the program makes the stems different heights?

Another hint

Where it says stem + i * 10, take away the part that uses i.

Try it yourselfYour edits are saved in this browser.

Add two more flowers that fit on the screen.

    Run your program. This ticks when your code does all of the above.

    For teachers

    Students read a longer program that combines the counter, a list, an update and a decision, find the lines that do each job, and change one part without breaking the rest. About 35 minutes.

    Open the teacher guide: answers, solutions, slides and a worksheet.

    Students will be able to

    Before class

    Run the garden, then do the Modify tasks yourself. The program is a loop with a long body: that length is what the next lesson fixes.

    Running the lesson

    1. Predict: read the code with the class, one comment at a time. The stem line uses the counter; ask what i is on the third pass.
    2. Run: students run it and watch which part the turtle is drawing. Ask: which lines draw a stem? Which draw a flower head?
    3. Investigate: the match task names each part's job. The bug is a reset: a value set inside the loop is set again every pass.
    4. Modify: students change one part at a time. If the extra flowers run off the screen, ask which line moves the start.

    The hard part (on purpose)

    Knowing which lines to change. Ask what job the new feature is, then which lines do that job.

    When students get stuck

    Stretch and support

    Stretch: Make every other flower head a different shape by adding a decision to the flower-head lines.

    Support: Colour or underline the lines for each job on paper before changing anything.

    Where it leads

    Next lesson names a group of lines (a function). The garden's long loop body is the first thing to name.

    What each step is for

    Now you can
    Next lesson2. FunctionsGive a group of commands a name, then use that name again and again.