Visit every item in a list in turn, without counting places.
A playlist doesn't count its songs. It just plays the next one until there are none left.
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:. What do you think c holds 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 c from one pass to the next.
Something in here trips the program up partway through.
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.
Before a hint: what kind of stuck is this? Try a strategy first
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 goes through the scores and colours each bar. Work it out on paper before you check.
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.
Before a hint: what kind of stuck is this? Try a strategy first
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.
Each bar is a day's rain, Monday first. Put in a week of your own, or one from the weather news. Then make the heavy days (more than 15 mm) a darker blue with an if inside the loop, so you can spot them at a glance.
Lesson done. You've finished every step you need.
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.