Week 7 · Session 1
Replacing an item, adding to the end, and building a list from nothing.
colors[1] = "amber" puts a new item where "yellow" wascolors[3] = "blue" is the same IndexErrorappend: Add to the Endcolors.append("blue") adds one item after the last onelen(colors) goes up by one, and the new item sits at position len(colors) - 10 with "white" and print the list againcolors[4]pattern = [] is a list with nothing in it yet. len(pattern) is 0append once per trip leaves one item per trip in the listPredict both printed lines before you press Run.
4 to 6 and predict againstep * 2 + 1 instead. What does the list hold now?2. The list calls that position 1. So the light they mean is leds[choice - 1]4 and there are three: leds[3] raises IndexErrorif 1 <= choice <= len(leds):- 1 and read the errorchoice to 1. Which item prints?if so it only runs when choice is between 1 and len(lights). Then try choice = 4leds = [Pin(15, Pin.OUT), Pin(14, Pin.OUT), Pin(13, Pin.OUT)]leds[0].on() lights the first one. leds[choice - 1].on() lights the one a person asked forfor led in leds: visits every light, and len(leds) says how many there areleds[choice - 1], after a range checkappend inside a loop, then lights it