Lesson 4-7 · Tuples and Lists: What You Now Know — Knowledge ChecksAnswer key
A program must remember every intersection where the robot found an obstacle, adding to it as the robot drives. What should it use?
Each position is a fixed pair, so a tuple. The collection grows as the robot drives, so a list. That is exactly what Module 5 does.
After these lines, what is total?
legs = [(2, 3), (1, 0)] total = 0 for leg in legs: x, y = leg total = total + x + yFirst pass: x is 2, y is 3, total becomes 5. Second pass: x is 1, y is 0, total becomes 6.
Given path = [(1, 0), (2, 0), (3, 0)], which line gives the y of the last position?
Three items have indexes 0, 1 and 2, so the last is path[2]. Its y is index 1. path[3] would be an IndexError.
What does scores hold after these three lines?
scores = [10, 20] backup = scores backup.append(30)
backup = scores stores the address that was in scores, not a copy of the list, so both variables lead to one list. Appending through either variable changes what both see.
A function is handed a list called blocked and runs blocked.append((2, 2)). After it returns, what does the caller's list hold?
The parameter holds the list's address, so append() changes the one list both sides share. Only assigning with = inside the function would leave the caller's list alone.