Skip to content

Week 9: while loops and sets

Until now, every loop you wrote knew how many times it would run: once per item in a list, once per character in a string, once per number in a range. This week's loops don't know. A while loop keeps going as long as a condition is true, so it fits the jobs where you can't count ahead of time: asking again until the user types something valid, or drawing again until a random draw is one you can use. You also meet the set, which holds each value at most once, and answers "have I seen this one already?" in a single in.

This week starts your third programming assignment (PA3), and it is the first one that runs for two weeks. It has four parts instead of three. Parts A and B use this week's while loops and sets. Parts C and D use next week's files and strings: they read the program's data from a file and write its results to another one. 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 Week09 folder inside ~/CS149, keep working on it there next week, and commit and push your work whenever you finish a part. 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. PA3 is one Gradescope assignment that runs for both weeks, and the same rule applies each week. Submit this week once Parts A and B work (C and D will simply show as not done yet), then submit again next week when the whole PA is finished.

Parts C and D are next week's

They need reading and writing files, which is next week's first class. Finish A and B this week, and leave C and D alone until then. Their docstrings are already in the file, so you can read ahead if you are curious.

Option 1: Gift Swap 🎁

Everyone writes their name on a slip, the slips go in a hat, and everyone draws one: that's who you're buying for. Simple, until you draw your own name, or the last person in line finds that the only slip left is theirs. Your code runs the hat. It signs everyone up, draws names until each one is a name it can use, and writes a card for every giver with their person's wish on it.

Start Gift Swap →

Option 2: Trivia Night ❓

Pull up a chair, the quiz is about to start. Your code is the host: it reads the questions from a file, picks them in random order without asking any question twice, and gives each player a couple of tries, without letting them burn a try on a blank answer or the same wrong guess twice. At the end of the night, it writes the results to a file.

Start Trivia Night →

What's the same in both

Each option is one file with four functions, one per part, plus a data file the program reads. The main block that runs the finished program is written for you.

Part Week Gift Swap Trivia Night What you practice
A 9 draw_name draw_question a while loop that draws again until a random draw is one you can use
B 9 sign_up ask a while loop that asks again until the input is valid, with a set of what you've seen
C 10 load_wishes load_questions reading a file one line at a time, and cleaning up each line
D 10 save_cards save_results writing a file one line at a time, with slicing

Name the three parts of every loop

Before you write a while loop, say out loud what it starts with (initialization), what keeps it going (condition), and what changes each time around (update). If nothing in the loop can make the condition false, the loop never ends, and that is the most common while bug there is.

When you're done

Commit and push your Week09 folder after each part:

git add -A
git commit -m "Finish PA3 Part A"
git push

When Part B works, look at your loop and point to its three parts: the initialization, the condition, and the update. Then ask yourself whether a for loop could have done the same job. If you can explain why it couldn't, you understand the difference between the two kinds of loop, and not just the syntax.