While Loops and Sentinel Values
Collect all numbers from the user into a list using a while loop that terminates when the user enters a sentinel value like 'done'.
Try it: while Loops, break and continue
How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.
How it works
- Evaluate the condition. If it is False, skip the body and continue after the loop.
- If it is True, run the body from the top, then go back and evaluate the condition again.
- continue skips the rest of the body and goes straight back to the condition check.
- break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
- If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.
Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.
Loading the simulation…
The Collection Problem
Suppose a program must collect several numbers, but it does not know in advance how many numbers the user will enter. A while loop can keep accepting values while the user continues to provide numbers. A sentinel value such as done gives the user a clear way to signal that no more numbers should be collected.
A sentinel value is a special input used to indicate that a repeated process should stop. In this pattern, done is the sentinel, while the other inputs are numeric values that belong in the list.
Tracing List Growth
Before analyzing the data, the program builds the list one number at a time. Consider the input sequence 6, 2, 9, 3, 5, then done. Each number is converted to a numeric type and appended. The sentinel is recognized as the stopping signal and is not appended.
| Input | Action | List after the action |
|---|---|---|
| 6 | Convert and append | [6] |
| 2 | Convert and append | [6, 2] |
| 9 | Convert and append | [6, 2, 9] |
| 3 | Convert and append | [6, 2, 9, 3] |
| 5 | Convert and append | [6, 2, 9, 3, 5] |
| done | Stop without appending | [6, 2, 9, 3, 5] |
The list changes only for numeric inputs; it remains complete when the sentinel ends the loop.
Analyzing the Completed List
The loop has one job: collect the numeric data. The max() and min() functions have a different job: analyze that data. They should be called after the loop exits, when the list is complete. For the collected values [6, 2, 9, 3, 5], max() finds 9 and min() finds 2.
Finding the Largest and Smallest Values
Determine the maximum and minimum values after the inputs 6, 2, 9, 3, 5, and done.
- 1Complete collection
The sentinel ends collection, leaving the numeric list [6, 2, 9, 3, 5].
- 2Find the maximum
Apply
max()to the completed list. The largest value is 9. - 3Find the minimum
Apply
min()to the completed list. The smallest value is 2.
The maximum is 9 and the minimum is 2.
Keeping the Sentinel Separate
The program must distinguish the sentinel from numeric input before adding anything to the list. A numeric input is converted to an int or float and then appended. The input done is handled as the stopping signal instead, so it never becomes part of the numeric data.
Debugging the Pattern
Common mistake Calling max() or min() inside the loop
What it looks likeThe program tries to compute a result while more user values may still be coming.
Why it failsThe list is not complete during collection, so the analysis is happening before all numbers have been gathered.
Do this insteadWait until the sentinel ends the loop, then call max() and min() on the completed list.
Common mistake Forgetting to convert input to a number
What it looks likeA numeric input is left as user input rather than being converted to an int or float.
Why it failsThe list is supposed to contain numbers collected from the user.
Do this insteadConvert each numeric input to a numeric type before appending it.
Common mistake Adding the sentinel to the list
What it looks likeThe value done is appended along with the numeric inputs.
Why it failsThe sentinel is a stopping signal, not one of the numbers to analyze.
Do this insteadRecognize done before the append step and end the loop without adding it.
Practice the Trace
Trace the list for the input sequence 4, 11, 7, done. Write the list after each numeric input, identify the list immediately after done is entered, and state which two functions should be called after the loop exits.
Hints
- Append only the numeric inputs.
- The sentinel ends collection but does not change the list.
- The two analysis functions are
max()andmin().
Practice Check
Review the input sequence 4, 11, 7, done.
- 1After 4
The list contains [4].
- 2After 11
The list contains [4, 11].
- 3After 7
The list contains [4, 11, 7].
- 4After done
The loop ends and the list remains [4, 11, 7].
Call max() and min() after the loop exits, using the completed list.
Key Takeaways
- 1Use a while loop to keep collecting numbers until the user enters a sentinel such as done.
- 2Convert each numeric input to an int or float before appending it to the list.
- 3Never append the sentinel value to the numeric list.
- 4Call
max()andmin()after the loop exits, when the list is complete. - 5When debugging, check the timing of the function calls, the numeric conversion, the sentinel handling, and whether the list contains data.
At a glance
- A sentinel value ends repeated input without becoming part of the collected data.
- The list grows only when a numeric value is converted and appended.
- The completed list should be analyzed after the while loop exits.
max()finds the largest value andmin()finds the smallest value in the completed list.- Common errors involve premature analysis, missing numeric conversion, and storing the sentinel.