Concepts / Working with Lists and Append

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'.

  • Programming

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.

append 6append 2append 9append 3append 5sentinel enteredEmpty list[]6[6]2[6, 2]9[6, 2, 9]3[6, 2, 9, 3]5[6, 2, 9, 3, 5]donestop; list unchanged
How does each user-entered number move into the list, and what happens next when the sentinel value is entered?
User inputActionList after the iteration
6Convert to a numeric type and append[6]
2Convert to a numeric type and append[6, 2]
9Convert to a numeric type and append[6, 2, 9]
3Convert to a numeric type and append[6, 2, 9, 3]
5Convert to a numeric type and append[6, 2, 9, 3, 5]
doneStop 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.

numeric inputafter doneanalyze after loopanalyze after loopUser inputnumbers or doneCollect numbersconvert and appendCompleted list[6, 2, 9, 3, 5]max()largest value: 9min()smallest value: 2
When are max() and min() called, and how do they use the completed list?

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.

inputyesnonumeric valuenext inputRead inputInput is doneStop loopkeep list unchangedConvert numbernumeric typeAppend valueadd to list
How does the program branch when the user enters a number compared with when the user enters done?

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

EASY

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?

  • [8, 1, 4, done]; largest 8; smallest 1
  • [8, 1, 4]; largest 8; smallest 1
  • [8, 1, 4]; largest 4; smallest 8
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

  1. Use a while loop to continue collecting input until a sentinel such as done is entered.
  2. Convert each numeric input to a numeric type before appending it to the list.
  3. Never append the sentinel value itself.
  4. Call max() and min() after the loop exits, when the list is complete.
  5. 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.