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'.
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.
Complete collection: The sentinel ends collection, leaving the numeric list [6, 2, 9, 3, 5].
Find the maximum: Apply max() to the completed list. The largest value is 9.
Find 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
Calling max() or min() inside the loop
The list is not complete during collection, so the analysis is happening before all numbers have been gathered.
Fix:
Wait until the sentinel ends the loop, then call max() and min() on the completed list.Forgetting to convert input to a number
The list is supposed to contain numbers collected from the user.
Fix:
Convert each numeric input to a numeric type before appending it.Adding the sentinel to the list
The sentinel is a stopping signal, not one of the numbers to analyze.
Fix:
Recognize 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() and min().
Practice Check
Review the input sequence 4, 11, 7, done.
After 4: The list contains [4].
After 11: The list contains [4, 11].
After 7: The list contains [4, 11, 7].
After 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
- Use a while loop to keep collecting numbers until the user enters a sentinel such as done.
- Convert each numeric input to an int or float before appending it to the list.
- Never append the sentinel value to the numeric list.
- Call max() and min() after the loop exits, when the list is complete.
- When debugging, check the timing of the function calls, the numeric conversion, the sentinel handling, and whether the list contains data.
Key Takeaways
- 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 and min() finds the smallest value in the completed list.
- Common errors involve premature analysis, missing numeric conversion, and storing the sentinel.