← Teacher guide

For each item

Students visit every item of a list with a for-each loop, and see that the loop name holds the item itself.

Python Foundations with Turtle · Choices and lists · about 30 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 8 min · Investigate 8 min · Modify 5 min · Mix it up 3 min · Make 6 min

Before class

Run the coloured squares and the bar chart. The idea to have ready: in for c in colours, c is the colour itself, not its place.

Running the lesson

  1. Predict: ask what c holds the first time round. The wrong options are the place number, the whole list, and nothing.
  2. Run: read the loop aloud as 'for each colour c in the list colours'. Ask: how many times did it run, and how do you know without counting?
  3. Investigate: the trace shows c changing each pass. The bug task uses c as a place; ask what colours[c] would mean.
  4. Mix it up: students work the count out on paper. Each score is tested as it is visited.
  5. Make: the bar chart is the same loop with heights instead of colours. A bar is a rectangle whose tall side is the number.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Remembering that c is the item, not its place. Ask what c holds on the first pass before running.

When students get stuck

What you will seeA question to askThe fix
An error on the pencolor line inside the loop.What does c hold? Is it a place or a colour?c is already the colour. Use pencolor(c), not pencolor(colours[c]).
The bars all start in the same place and overlap.What does the turtle do after finishing one bar?Lift the pen and move forward past the bar before drawing the next one.
A student prefers range(len(...)).Do you need the place in this loop?If only the item matters, use for-each. Use the place when you need it, for example to pick from two lists at once.

Answers to the checks

  1. Predict: On the first time round the loop, what does c hold?
    "red", the first colour itself. In for c in colours, c is each item in turn: first "red", then "orange", and so on. It is not a place number.
  2. What does c hold?: Fill in what c holds on each pass.
    orange, yellow, green, blue
    c holds each item in turn, in the order they are in the list.
  3. Spot the bug: This program should draw three circles, but it stops with an error. Click the line that causes it.
    line 6
    c is already a colour, such as "red". colours[c] asks for the item in place "red", which makes no sense. Use pencolor(c).
  4. Mix it up: scores = [45, 72, 90, 38, 66] and pass_mark = 50. How many bars are green?
    3
    72, 90 and 66 are at least 50, so three bars are green. 45 and 38 are red.

Solutions

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

Modify

from turtle import *

colours = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
size = 40

penup()
goto(-190, 0)
pendown()

for c in colours:
    fillcolor(c)
    begin_fill()
    for side in range(4):
        forward(size)
        right(90)
    end_fill()
    penup()
    forward(size + 10)
    pendown()

Make

from turtle import *

heights = [30, 80, 50, 100, 60]

penup()
goto(-100, -100)
pendown()

for h in heights:
    pendown()
    forward(20)
    left(90)
    forward(h)
    left(90)
    forward(20)
    left(90)
    forward(h)
    left(90)
    penup()
    forward(30)

Stretch and support

Stretch: Draw the bar chart with each bar a different colour, using a second list of colours alongside the heights.

Support: Give the loop with the first two passes traced, and ask the student to continue the table.

Exit ticket

sizes = [10, 20, 30]. for s in sizes: forward(s). How far does the turtle go on the second pass?

20. On the second pass s holds the second item, 20.

What each step is for

Where it leads

Lists and loops return in Catch the turtle, where a list holds the path, and in the control panel. Course B starts by reading a longer program that uses everything from this course.