Working with Lists and Append
The sentinel value ends collection. It is a control signal, not one of the numbers in the list.
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.
- 1Complete collection
After the sentinel is entered, the list contains [6, 2, 9, 3, 5].
- 2Find the largest
Call
max()on the completed list. The largest value is 9. - 3Find 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.
List before the sentinel: [6, 2, 9, 3, 5]
The list is complete after the last numeric input.
List after the sentinel: [6, 2, 9, 3, 5]
Entering done stops collection; no sentinel is added.
Mistakes That Distort the Result
Common mistake Calling max() or min() inside the loop
What it looks likeThe program tries to calculate the largest and smallest values while the list is still being built.
Why it failsThe intended calculation uses the completed list after the loop exits. During collection, the data is not yet complete.
Do this insteadBuild the list first, stop when the sentinel is entered, and call max() and min() afterward.
Common mistake Forgetting to convert input to a number
What it looks likeA user-entered number is treated as input text instead of being converted to an int or float.
Why it failsThe collection task requires numeric values in the list.
Do this insteadConvert each non-sentinel input to a numeric type before appending it.
Common mistake Appending the sentinel
What it looks likeThe value done is placed in the same list as the numbers.
Why it failsThe sentinel is a stopping signal, not a number to analyze.
Do this insteadCheck 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()andmin()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
[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
- 1Use a while loop to continue collecting input until a sentinel such as done is entered.
- 2Convert each numeric input to a numeric type before appending it to the list.
- 3Never append the sentinel value itself.
- 4Call
max()andmin()after the loop exits, when the list is complete. - 5When debugging, check the timing of
max()andmin(), numeric conversion, sentinel handling, and the possibility of an empty list.
At a glance
- 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()andmin()should be called after collection finishes.- Tracing the list after every input exposes errors in conversion, appending, and loop control.