Week 6: for loops, lists, and ranges
Last week your programs got cut into functions, and every function did its work once per call.
This week brings two ideas that arrive together, because neither is much use without the other.
A list holds any number of values in order, under one name.
A for loop runs the same block of statements once for each value in a list, or once for each number in a range, however many there turn out to be.
This week's practice is also a new shape. Instead of six small exercises, it is your first programming assignment (PA): one program of three functions that build on each other, in Parts A, B, and C, so the last one only works when the first two do. There are two options, and you pick one. They are the same size and practice the same skills, so go with the story you like better.
Save your PA in the Week06 folder inside ~/CS149, and commit and push your work when you finish.
Each option has an assignment on Gradescope, and you can submit as many times as you like, so check your work as you go.
Submit early to Gradescope
If you don't submit to Gradescope and get a nonzero score, you will receive a reflection grade of no higher than 15/30 points.
Loops this week are for loops, and nothing else
Python has a second kind of loop, while, which is Week 9, and neither option needs it.
The same goes for anything that does a loop's job without a loop, like sum(), max(), slicing, or a list comprehension: Gradescope rejects them, because writing the loop yourself is the whole point of the week.
Option 1: Chutes and Ladders 🎲
You know the game: roll, move, climb a ladder, groan down a chute, roll again. Your code is the referee that never looks away. You build the board as a list, move a player one roll at a time (including the annoying bounce-back when you overshoot the finish), and replay a whole game from a list of rolls to find the turn someone wins.
Option 2: Robot Vacuum 🤖
A robot vacuum is not smart; it just follows orders. Drive 3 spots right, stop, vacuum. Drive 5 left, bonk the wall, bounce back, vacuum. You build the hallway as a list of dirty spots, teach the robot to bounce off both walls, and run a list of moves to find out when the hall is finally clean.
What's the same in both
Each option is one file with three functions, one per part, and the main block that runs the finished program is written for you.
| Part | Chutes and Ladders | Robot Vacuum | What you practice |
|---|---|---|---|
| A | make_track |
make_hall |
building a list with a loop, then changing it by index |
| B | move |
step |
a rule with several cases, including bouncing off an end |
| C | play |
clean |
a loop that remembers where you are and what happened |
One part at a time
Write Part A, test it in the Shell with the examples on your option's page, and submit it to Gradescope before you start Part B.
When you're done
Commit and push your Week06 folder:
Then trace your Part C function on paper for a short input of your own — three or four moves, one of which bounces — and check your trace against what your program returns. If the two agree, you understand your own loop, and not just that it passes.