← Python Foundations with Turtle

For each item

Visit every item in a list in turn, without counting places.

A playlist plays each song, a printer prints each page and a game moves each enemy. They all work through a list, one item at a time.

Last lesson you picked items by their place. Often you don't care about the place: you want to do something with each item.

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

for_each.py
from turtle import *

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

penup()
goto(-130, 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()

The new line is for c in colours:. Read the code. What does c hold the first time round?

On the first time round the loop, what does c hold?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction.

Line What it does Why
for c in colours: Runs the indented lines once for each item No range, no counting
c Holds the current item: "red", then "orange" ... It is the item itself, not its place
fillcolor(c) Uses the colour for this square Five items, five squares
for_each.py
from turtle import *

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

penup()
goto(-130, 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()
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

for_each.py
from turtle import *

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

penup()
goto(-130, 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()

Follow the name from pass to pass.

Here is a version where something is wrong.

Fill in what c holds on each pass.

passc
1red
2
3
4
5

This program should draw three circles, but it stops with an error. Click the line that causes it.

ModifyChange the code on purpose and check the result.

Modify

Add two more colours to the list. Move the start so they all fit on the screen.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Each colour is a word in quotes, separated from the next by a comma.

Another hint

Seven squares are wider than five. Move the start further left, with goto.

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

Mix it up

This program visits each score and decides its colour. Work out the answer before you run it.

for_each.py
from turtle import *

scores = [45, 72, 90, 38, 66]
pass_mark = 50
width = 30

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

for score in scores:
    if score >= pass_mark:
        fillcolor("green")
    else:
        fillcolor("red")
    begin_fill()
    forward(width)
    left(90)
    forward(score)
    left(90)
    forward(width)
    left(90)
    forward(score)
    left(90)
    end_fill()
    penup()
    forward(width + 10)
    pendown()

scores = [45, 72, 90, 38, 66] and pass_mark = 50. How many bars are green?

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Draw a bar chart: one bar for each number in heights, the height of the bar equal to the number.

Try it yourselfYour edits are saved in this browser.
Need a hint?

One bar for each number in the list: for h in heights does that, and each time round h is the next height.

Another hint

A bar is a rectangle: forward(20), left(90), forward(h), left(90), forward(20), left(90), forward(h), left(90). Then move on before the next bar.

Draw a bar for each number using a for-each loop.

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

    For teachers

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

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

    Students will be able to

    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.

    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

    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.

    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.

    What each step is for

    Now you can
    Next lesson11. Challenge: a tileRead a tile pattern, change one thing and predict the result, then make a tile of your own.