Students visit every item of a list with a for-each loop, and see that the loop name holds the item itself.
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.
Predict and Run 8 min · Investigate 8 min · Modify 5 min · Mix it up 3 min · Make 6 min
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.
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.
Here is the finished program running. By the end of the lesson you will write one like it.
Run the finished example on the projector before anything else.
One student walks along a row of colour cards and says what c holds at each. Compare with the index version from last lesson.
Make the idea physical before it goes back to the screen.
Remembering that c is the item, not its place. Ask what c holds on the first pass before running.
| What you will see | A question to ask | The 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. |
One possible solution for each task. Other correct programs are fine.
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()
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: 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.
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.
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.