Concepts / Built-in Functions: max() and min()

Built-in Functions: max() and min()

The loop builds the list. The calls to max() and min() happen after the loop, when the list contains all collected numbers.

  • Programming

From Input to Answer

A program that finds the largest and smallest values from user input has two separate jobs. First, it collects numbers into a list. Second, after collection is complete, it examines that list with max() and min(). Keeping these jobs separate makes the program easier to trace and helps ensure that the sentinel value, such as done, is not treated as a number.

Key point

The loop builds the list. The calls to max() and min() happen after the loop, when the list contains all collected numbers.

What do you think happens?

A user enters 6, 2, 9, 3, 5, and then done. When should max() and min() be called?

  • After every number is entered

  • When the first number is entered

  • After done causes the loop to exit

Reveal answer
Answer:

After done causes the loop to exit

The list must be complete before max() and min() are used to find the largest and smallest values.

Looping Until done

The input process repeats while the user is entering numbers. Each numeric input is converted to int or float and appended to the list. The sentinel value marks the end of the process: it tells the loop to stop, and it is not appended to the numeric list.

StartRead inputInput is doneAppend numberRead next inputCall max() and min()FinishNoYes
How does control flow change when the user enters a number versus the sentinel value, and when does the loop stop?

The important branch is the sentinel check. A number follows the append path and causes another input to be read. The sentinel follows the exit path. Because it exits before the append step, it never becomes part of the list examined by max() and min().

Tracing the Growing List

Consider the input sequence 6, 2, 9, 3, 5, and done. The list starts empty. Each number changes the list by adding one converted numeric value. When done is entered, the list does not change; instead, the loop exits. The completed list is therefore [6, 2, 9, 3, 5] immediately before max() and min() are called.

Input begins[]6[6]2[6, 2]9[6, 2, 9]3[6, 2, 9, 3]5[6, 2, 9, 3, 5]doneloop exitsappendappendappendappendappendsentinel
How does the list change after each number is entered, and what does it contain immediately before max() and min() are called?
Worked example

Six Inputs, One Completed List

Trace the list and identify the values found by max() and min() after the inputs 6, 2, 9, 3, 5, and done.

  1. 1
    Start

    The list is empty before any numeric input has been collected.

  2. 2
    Collect numbers

    The values 6, 2, 9, 3, and 5 are converted to numeric values and appended in that order.

  3. 3
    Receive the sentinel

    done tells the loop to exit. It is not appended to the list.

  4. 4
    Analyze the completed list

    max() and min() are called only after the loop exits, using the complete list [6, 2, 9, 3, 5].

Result

max() identifies 9 as the largest value, and min() identifies 2 as the smallest value.

Using the Completed List

max() and min() both examine the same completed list, but they answer different questions. max() identifies the largest value in the list, while min() identifies the smallest value. Their position in the process matters: they are used after the while loop has finished collecting input, not during each repetition.

Completed list[6, 2, 9, 3, 5]max()9min()2largest valuesmallest value
How do max() and min() examine the same list to identify its largest and smallest values?
Key point

Calling max() or min() inside the input loop analyzes a partial list. The intended process is to finish collecting numbers first and analyze the complete list afterward.

Keeping the Sentinel Out

The sentinel has a control-flow role, not a data role. When the user enters done, the program should use it to leave the loop. It should not convert done to a number and should not append it to the list. The list passed to max() and min() must contain the collected numeric values only.

InputdoneNumeric list[6, 2, 9, 3, 5]Loop exitstopnot appended
Where does the sentinel value go in the control flow, and why is it not added to the list of numbers?

Debugging Collection Problems

Most problems in this pattern come from confusing input control with numeric data. Check each input at the point where it is received: determine whether it is the sentinel, and convert every non-sentinel input to int or float before appending it.

  • Common mistake

    Calling max() or min() inside the loop.

    What it looks like

    The program tries to compute a result after each individual input.

    Why it fails

    The list is still being built, so the analysis happens before all user data has been collected.

    Do this instead

    Wait until the loop exits, then call max() and min() on the complete list.

  • Common mistake

    Forgetting to convert user input to a number.

    What it looks like

    A received value is appended without converting it to int or float.

    Why it fails

    The list is intended to contain numeric values, but the input has not been converted to a numeric type.

    Do this instead

    Convert each non-sentinel input to int or float before appending it.

  • Common mistake

    Adding the sentinel to the list.

    What it looks like

    done is appended along with the numeric inputs.

    Why it fails

    The sentinel marks termination; it is not one of the numbers being analyzed.

    Do this instead

    Check for the sentinel before conversion and appending, then exit the loop when it appears.

  • Common mistake

    Calling max() or min() before any numbers have been collected.

    What it looks like

    The loop ends immediately because the first input is done, but the program still tries to analyze the list.

    Why it fails

    The list has no collected numbers to analyze.

    Do this instead

    Handle the empty-list case before calling max() and min().

User inputSentinel checkConvert to int orfloatAppend to listReview input handlingEmpty listCall max() and min()numbersentinelno collected numberscompleted listrepeat
What happens to the program when input is not converted to a number or when max() and min() are called before any numbers have been collected?

Practice the Trace

EASY

Trace this input sequence: 12, 4, 18, 7, done. Write the list after each numeric input. Then identify which value max() finds and which value min() finds. Finally, explain why done is absent from the list.

Hints
  • Start with an empty list.
  • Append only the numeric inputs.
  • The sentinel ends the loop and is not appended.
  • Call max() and min() only after the final list is complete.
MEDIUM

A student says, “I will call max() and min() after every input so the program always knows the current answer.” Explain why this does not follow the intended collection pattern, and describe where the calls should be placed instead.

Hints
  • Ask whether the list is complete after each input.
  • Separate collecting data from analyzing the completed list.

Process Checklist

  1. 1
    Create a list for the numeric inputs.
  2. 2
    Read user input repeatedly with a while loop.
  3. 3
    Check whether the input is the sentinel, such as done.
  4. 4
    If it is a number, convert it to int or float and append it to the list.
  5. 5
    When the sentinel appears, exit the loop without appending it.
  6. 6
    After the loop, call max() and min() on the completed list.
  7. 7
    Handle the case where the list contains no collected numbers before analyzing it.

Key Takeaways

  • Use a while loop to collect numbers until a sentinel such as done is entered.
  • Convert every non-sentinel input to int or float before appending it to the list.
  • Do not add the sentinel to the numeric list.
  • Call max() and min() after the loop exits and the list is complete.
  • Debug by checking input conversion, sentinel exclusion, call placement, and the empty-list case.

Related concepts

More concepts starting with B