Working with Lists and Append
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 Input to a Complete List
A program that collects numbers from a user has two separate jobs. First, it builds a list by accepting numbers one at a time. Second, after the user signals that there are no more numbers, it examines the completed list to find its largest and smallest values. Keeping these jobs separate makes the program easier to trace and helps ensure that max() and min() are used at the correct time.
The sentinel value ends collection. It is a control signal, not one of the numbers in the list.
Watching append Build the List
Imagine the user enters 6, then 2, then 9, then 3, then 5, and finally done. Each number is converted to a numeric type and appended to the list. The list therefore grows after every numeric entry. When done is entered, collection stops, and done is not appended.
| User input | Action | List after the iteration |
|---|---|---|
| 6 | Convert to a numeric type and append | [6] |
| 2 | Convert to a numeric type and append | [6, 2] |
| 9 | Convert to a numeric type and append | [6, 2, 9] |
| 3 | Convert to a numeric type and append | [6, 2, 9, 3] |
| 5 | Convert to a numeric type and append | [6, 2, 9, 3, 5] |
| done | Stop collecting; do not append | [6, 2, 9, 3, 5] |
The list changes only after numeric input. The sentinel leaves the completed list unchanged.
Separating Collection from Analysis
The input loop is responsible for collecting data. During the loop, the program converts each number to a numeric type and appends it to the list. It does not yet call max() or min(). Those functions are called after the loop exits, when the list is complete. max() finds the largest value in the completed list, while min() finds the smallest value.
Finding the two extremes
The collected numbers are 6, 2, 9, 3, and 5. Determine the results after the user enters done.
Complete collection: After the sentinel is entered, the list contains [6, 2, 9, 3, 5].
Find the largest: Call max() on the completed list. The largest value is 9.
Find the smallest: Call min() on the completed list. The smallest value is 2.
The maximum is 9 and the minimum is 2.
Tracing the Sentinel Branch
Each input has one of two roles. A numeric input belongs in the list, so it is converted to a numeric type and appended. The sentinel belongs to the control flow, so it ends the loop and remains outside the list. This distinction is the key to tracing the program correctly.
Mistakes That Distort the Result
Calling max() or min() inside the loop
The intended calculation uses the completed list after the loop exits. During collection, the data is not yet complete.
Fix:
Build the list first, stop when the sentinel is entered, and call max() and min() afterward.Forgetting to convert input to a number
The collection task requires numeric values in the list.
Fix:
Convert each non-sentinel input to a numeric type before appending it.Appending the sentinel
The sentinel is a stopping signal, not a number to analyze.
Fix:
Check for the sentinel before conversion and append; stop without adding it.
If the user enters the sentinel before entering any numbers, the list remains empty. This is an important case to check while debugging because max() and min() are intended to operate after numbers have been collected. The source guidance emphasizes completing the numeric collection before applying these functions.
Practice the State Changes
Trace this input sequence: 8, 1, 4, done. Write the list after each input. Then identify the values that max() and min() should find after the loop exits.
Hints
- Start with an empty list.
- Append each number, but do not append done.
- Call max() and min() only after the final list is complete.
What do you think happens?
After the inputs 8, 1, 4, done, what is the completed list, and what do max() and min() find?
Reveal answer
Answer: [8, 1, 4]; max() finds 8 and min() finds 1.
The three numeric inputs are appended. The sentinel ends collection without entering the list. The largest and smallest values are then found from the completed list.
What to Remember
- Use a while loop to continue collecting input until a sentinel such as done is entered.
- Convert each numeric input to a numeric type before appending it to the list.
- Never append the sentinel value itself.
- Call max() and min() after the loop exits, when the list is complete.
- When debugging, check the timing of max() and min(), numeric conversion, sentinel handling, and the possibility of an empty list.
Key Takeaways
- A sentinel-controlled loop separates numeric data from the signal that ends collection.
- append() grows the list one numeric value at a time.
- The sentinel is not part of the list.
- max() and min() should be called after collection finishes.
- Tracing the list after every input exposes errors in conversion, appending, and loop control.