Input Validation and Type Conversion
Collect all numbers from the user into a list using a while loop that terminates when the user enters a sentinel value like 'done'.
From Responses to Results
A program that collects numbers from a user has two separate jobs. First, it must gather valid numeric values into a list. Second, after the collection is finished, it must analyze that completed list. Input validation and type conversion connect these two jobs: the program recognizes a special stopping response, converts ordinary numeric responses to a numeric type, and keeps the sentinel value out of the data.
The central sequence is collect, convert, append, stop at the sentinel, then compute the maximum and minimum.
Following the Sentinel Path
A sentinel-controlled loop keeps requesting input while the user provides numbers. In this example, the sentinel is the text "done". Each ordinary response follows the numeric path: it is converted and added to the list. The sentinel follows the stopping path: it ends the loop and is never added.
The important boundary is the sentinel check. A response equal to "done" is a control signal, not a number. It tells the loop to stop, so it must not become a list item. An invalid response is different from the sentinel: it is not a valid number and is not the intended stopping signal, so the input process must deal with it as invalid rather than treating it as collected data.
Watching the List Grow
Consider the source example in which the user enters 6, then 2, then 9, then 3, then 5, and finally "done". The list begins empty. Every numeric response is converted and appended, so the list grows one item at a time. When "done" arrives, the list contains all five numbers and does not contain the sentinel.
Tracing the Collected Values
Trace the list for the inputs 6, 2, 9, 3, 5, and done.
Start: The collection list is empty because no numeric input has been accepted yet.
Accept 6: Convert 6 to a numeric value and append it, producing [6].
Accept 2: Convert 2 and append it, producing [6, 2].
Accept 9: Convert 9 and append it, producing [6, 2, 9].
Accept 3 and 5: Append the remaining numeric values in order, producing [6, 2, 9, 3, 5].
Receive done: Stop collecting. The sentinel is not appended, so the list remains [6, 2, 9, 3, 5].
The completed list is [6, 2, 9, 3, 5].
Converting Before Analysis
User input must be converted to a numeric type such as int or float before it is appended. This preserves the meaning of the collected data as numbers. The sentinel is handled separately: it is recognized as the stopping value and is not converted into a list item.
Conversion belongs before the append operation. If the program appends first, the list may contain the original user response rather than the intended numeric value. The later max() and min() operations are supposed to analyze the collected numbers, so the list should contain numeric values only.
Computing After Collection
The timing of max() and min() matters. During the loop, the program is building the list. After the sentinel causes the loop to exit, the list is complete, and max() and min() are called on that completed list. For the collected values 6, 2, 9, 3, and 5, max() identifies 9 as the largest value and min() identifies 2 as the smallest value.
Diagnosing Incorrect Results
Most problems in this pattern come from breaking the boundary between control input and data, or from analyzing the list at the wrong time. Use the list contents and the point at which each operation occurs as your primary debugging evidence.
Forgetting to convert input to a number
The list does not contain the numeric values required for the intended maximum and minimum calculation.
Fix:
Convert each numeric response to int or float before appending it.Adding the sentinel to the list
The sentinel is a control value, not one of the numbers being collected.
Fix:
Check for the sentinel before conversion and append only ordinary numeric inputs.Calling max() or min() inside the loop
The calculation sees only a partial list and not the complete collection.
Fix:
Call both functions after the loop exits.Analyzing an empty list
There are no collected values from which to identify a largest or smallest number.
Fix:
Validate that at least one numeric value was collected before requesting maximum and minimum results.
When debugging, inspect the list immediately before the loop ends. It should contain numeric values only, should not contain the sentinel, and should represent every valid number entered. Then verify that max() and min() are called after this point rather than during collection.
Practice the Sequence
Trace a collection process for the inputs 4, 10, 1, and done. Write the list after each numeric input. Then identify the list passed to max() and min(), and state the largest and smallest values.
Hints
- Do not add done to the list.
- The list changes only after a valid numeric input is converted and appended.
- Call max() and min() only after the final list is complete.
Debug this design in words: the program appends every response, including done, and calls max() after each append. Identify two separate problems and describe where each correction belongs in the sequence.
Hints
- Separate the stopping signal from numeric data.
- Compare the timing of analysis with the point at which the list becomes complete.
Key Takeaways
- Use a while loop to continue collecting input until the sentinel value is entered.
- Convert each numeric response to int or float before appending it to the list.
- Keep the sentinel out of the list because it controls the loop rather than representing data.
- Call max() and min() after the loop exits, when the list is complete.
- Debug by checking the list contents, conversion step, sentinel handling, and function timing.
Key Takeaways
- A sentinel-controlled loop separates valid numeric data from the stopping signal.
- Type conversion must happen before a numeric response is appended to the list.
- The list grows during collection and is complete immediately before the sentinel ends the loop.
- max() and min() should be called after collection, not inside the loop.
- The most useful debugging check is whether the final list contains all numbers, no sentinel, and no unconverted input.