Concepts / While Loops and Sentinel Values

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

  • Programming
Interactive lab

Try it: while Loops, break and continue

How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.

How it works

  1. Evaluate the condition. If it is False, skip the body and continue after the loop.
  2. If it is True, run the body from the top, then go back and evaluate the condition again.
  3. continue skips the rest of the body and goes straight back to the condition check.
  4. break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
  5. If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.

Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.

Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.

Educational simulation

Loading the simulation…

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.

Definition

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.

User inputNumberAppend numberdoneLoop endsvalue receivedyessentinel checkyes
What happens after each user input, and how does entering done stop the loop without adding another number?

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.

InputActionList after the action
6Convert and append[6]
2Convert and append[6, 2]
9Convert and append[6, 2, 9]
3Convert and append[6, 2, 9, 3]
5Convert and append[6, 2, 9, 3, 5]
doneStop without appending[6, 2, 9, 3, 5]

The list changes only for numeric inputs; it remains complete when the sentinel ends the loop.

List[]List[6]List[6, 2]List[6, 2, 9]List[6, 2, 9, 3]List[6, 2, 9, 3, 5]done62935sentinel
How does the list change after each number is entered, and what does it contain immediately before the loop ends?

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.

Worked example

Finding the Largest and Smallest Values

Determine the maximum and minimum values after the inputs 6, 2, 9, 3, 5, and done.

  1. 1
    Complete collection

    The sentinel ends collection, leaving the numeric list [6, 2, 9, 3, 5].

  2. 2
    Find the maximum

    Apply max() to the completed list. The largest value is 9.

  3. 3
    Find the minimum

    Apply min() to the completed list. The smallest value is 2.

Result

The maximum is 9 and the minimum is 2.

Collect numbersdoneCompleted list[6, 2, 9, 3, 5]max()9min()2repeated inputloop exitsanalyzeanalyze
When are max() and min() called, and how do they use the completed list?

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.

InputNumeric valueConvert to numberNumeric listdoneinspectyesappenddone
How does the program distinguish the sentinel value done from numbers, and why should done not appear in the final list?

Debugging the Pattern

  • Common mistake

    Calling max() or min() inside the loop

    What it looks like

    The program tries to compute a result while more user values may still be coming.

    Why it fails

    The list is not complete during collection, so the analysis is happening before all numbers have been gathered.

    Do this instead

    Wait until the sentinel ends the loop, then call max() and min() on the completed list.

  • Common mistake

    Forgetting to convert input to a number

    What it looks like

    A numeric input is left as user input rather than being converted to an int or float.

    Why it fails

    The list is supposed to contain numbers collected from the user.

    Do this instead

    Convert each numeric input to a numeric type before appending it.

  • Common mistake

    Adding the sentinel to the list

    What it looks like

    The value done is appended along with the numeric inputs.

    Why it fails

    The sentinel is a stopping signal, not one of the numbers to analyze.

    Do this instead

    Recognize done before the append step and end the loop without adding it.

Collected list[6, 2, done]Numeric list[6, 2]Input valuenumeric input not convertedConverted valueint or floatexclude sentinelconvert before append
What changes when the list is empty, the sentinel is stored, or numeric input remains unconverted?

Practice the Trace

EASY

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().
Worked example

Practice Check

Review the input sequence 4, 11, 7, done.

  1. 1
    After 4

    The list contains [4].

  2. 2
    After 11

    The list contains [4, 11].

  3. 3
    After 7

    The list contains [4, 11, 7].

  4. 4
    After done

    The loop ends and the list remains [4, 11, 7].

Result

Call max() and min() after the loop exits, using the completed list.

Key Takeaways

  1. 1
    Use a while loop to keep collecting numbers until the user enters a sentinel such as done.
  2. 2
    Convert each numeric input to an int or float before appending it to the list.
  3. 3
    Never append the sentinel value to the numeric list.
  4. 4
    Call max() and min() after the loop exits, when the list is complete.
  5. 5
    When debugging, check the timing of the function calls, the numeric conversion, the sentinel handling, and whether the list contains data.

At a glance

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

Related concepts

More concepts starting with W