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.
Make your prediction first. Help opens once you've locked it in.
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?
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 |
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()
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.
| pass | c |
|---|---|
| 1 | red |
| 2 | |
| 3 | |
| 4 | |
| 5 |
This program should draw three circles, but it stops with an error. Click the line that causes it.
Add two more colours to the list. Move the start so they all fit on the screen.
Each colour is a word in quotes, separated from the next by a comma.
Seven squares are wider than five. Move the start further left, with goto.
This program visits each score and decides its colour. Work out the answer before you run it.
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?
Draw a bar chart: one bar for each number in heights, the height of the bar equal to the number.
One bar for each number in the list: for h in heights does that, and each time round h is the next height.
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.
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.
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.
Remembering that c is the item, not its place. Ask what c holds on the first pass before running.
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.
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.