Students keep many values in one list, pick one by its place, and count them with len, learning that places start at 0.
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 10 min · Investigate 10 min · Modify 6 min · Mix it up 4 min
Run the coloured shape. The idea to have ready: the first item in a list is in place 0, so a list of 5 items has places 0 to 4. Asking for place 5 stops the program with an IndexError.
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.
Five students hold colour cards in places 0 to 4. Call colours[2]; the student steps forward. Then call colours[5].
Make the idea physical before it goes back to the screen.
Places start at 0, so the last place is one less than the length. Ask how many items there are and what the last place number is.
| What you will see | A question to ask | The fix |
|---|---|---|
| An IndexError: list index out of range. | How many items are in the list, and what is the last place number? | The last place is one less than the length. Loop with range(len(colours)) so the count always matches. |
| A NameError, or the colour name is printed instead of used. | Does every colour have quotes round it, with commas between them? | Text in a list needs quotes: ["red", "blue"]. |
| A student expects colours[1] to be red. | What is the place of the very first item? | The first place is 0. colours[0] is red, colours[1] is orange. |
One possible solution for each task. Other correct programs are fine.
from turtle import *
colours = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
pensize(8)
for i in range(len(colours)):
pencolor(colours[i])
forward(50)
right(360 / len(colours))
from turtle import *
colours = ["pink", "orange", "yellow", "green", "blue"]
pensize(8)
for i in range(len(colours)):
pencolor(colours[i])
forward(50)
right(72)
Stretch: Make a second list of sizes and use sizes[i] alongside colours[i], so each side has its own colour and length.
Support: Give students the list on paper with the places written under each item, and ask them to point to colours[2] before running the program.
colours = ["red", "blue", "green", "gold"]. What is colours[1], and what is the place of "gold"?
colours[1] is "blue" (counting starts at 0). "gold" is in place 3.
Next lesson visits every item without counting places: a for-each loop. Lists return in the game's path and the buttons of a control panel. Playlists, class rolls and leaderboards are lists.