PCC·07 · Chapter record
User Input and while Loops
Chapter 7 — programs that keep running until you tell them to stop
Five sessions on interactive programs: reading input(), while loops, flags/break/continue, using while loops to shuffle items between lists, and filling a dictionary from repeated user input. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.
input() & Numerical Input
Concept
input()pauses the program and waits for typed text, then hands it back as a string — always, even if it looks like a number.- Give
input()a clear prompt as its argument, with a trailing space so the user's typed text doesn't run into it. - Convert with int() (or float()) before doing math or numeric comparisons — comparing a raw string to a number raises a TypeError.
- The modulo operator % returns the remainder of a division —
number % 2 == 0is the classic even/odd test.
Key Terms
- input()
- pauses the program and returns whatever the user typed, as a string
- prompt
- the message passed to input() telling the user what to enter
- % (modulo)
- returns the remainder of a division
Exercise
Ask the user what kind of rental car they'd like, then print a message about it.
Ask how many people are in a dinner party. If it's more than 8, print that they'll have to wait; otherwise say their table is ready.
while Loops
Concept
- A while loop keeps running as long as its condition stays True — unlike a for loop, which runs once per item in a collection.
- Give the loop variable a starting value before the loop, and change it somewhere inside the loop, or it will never stop.
while True:loops forever on purpose — you exit it deliberately, usually with break.
Key Terms
- while loop
- repeats a block as long as its condition is True
- infinite loop
- a loop whose condition never becomes False — usually a bug
Exercise
Write a while loop that counts down from 10 to 1, then prints "Liftoff!".
Write a while loop that keeps doubling a number starting from 1, stopping once it exceeds 1000.
Flags, break & continue
Concept
- A common pattern: loop
while message != 'quit':, givingmessagea starting value before the loop so Python has something to compare on the very first pass. - A flag (like
active = True) is a single variable that controls the whole loop — cleaner than cramming every stop condition into the while line itself. breakexits a loop immediately, skipping whatever code is left inside it.continuejumps back to the top of the loop right away, skipping the rest of that pass without exiting the loop.
Key Terms
- flag
- a variable (often True/False) used to control whether a loop keeps running
- break
- exits the loop immediately
- continue
- skips to the next pass of the loop immediately
Exercise
Write a loop that asks people their favorite pizza topping and prints a message for each — until someone enters 'quit'.
Using continue, print only the numbers from 1 to 20 that are NOT multiples of 3.
while Loops with Lists
Concept
- Modifying a list while looping over it with
forcauses trouble — use a while loop instead when you need to move or remove items as you go. while unconfirmed_users:keeps running as long as the list has anything left in it — an empty list is falsy, so the loop stops naturally..pop()pulls one item off a list each pass, letting you move it into another list — the two lists shrink and grow in tandem.while value in list: list.remove(value)removes every instance of a repeated value, not just the first one.
Key Terms
- while <list>
- True while the list has at least one item, False once empty
Exercise
Start with a list of unconfirmed users. Use a while loop to move each one into a confirmed_users list, printing a verification message each time.
Make a list of foods with one item repeated several times. Use a while loop to remove every instance of that item.
Filling a Dictionary with User Input
Concept
- Each pass through a while loop can prompt for as much input as you want, and store it however you like — here, into a dictionary keyed by name.
- Ask a yes/no question each round and use the answer to flip a flag, ending the loop naturally when the user says "no."
- Once the loop ends, loop the finished dictionary with
.items()to report everything that was collected.
Exercise
Run the poll widget below for at least two people, then read the final results. How does the dictionary end up storing more than one person's answer?