Concepts / Loops and User Input

Loops and User Input

The important design question is not only how to calculate the average. It is also whether the program will need the original numbers after the loop.

  • Programming

Two Ways to Keep Input

When a program collects several numbers from a user and needs their average, it can process the numbers in two main ways. The running-total method keeps only a sum and a count while the loop runs. The list method stores every number in a list and analyzes the list after the loop. Both methods can produce the same average, but they preserve different amounts of information.

Key point

The important design question is not only how to calculate the average. It is also whether the program will need the original numbers after the loop.

Running Total in Motion

The running-total method maintains two pieces of state during the loop. The total records the sum of the numbers entered so far. The count records how many numbers have been entered so far. For each new number, the program adds that number to total and increases count by 1. When input collection ends, the average is calculated by dividing total by count.

Start4total 4; count 1Average20 divided by 37total 11; count 29total 20; count 3enter 4enter 7enter 9divide total by count
How do the running total and count change after each user input, and how is the average computed at the end?
Worked example

Tracing three inputs

A user enters the numbers 4, 7, and 9. Trace the running-total method.

  1. 1
    Before the loop

    The method begins with a total of 0 and a count of 0.

  2. 2
    After 4

    Add 4 to the total, giving total 4. Increase the count to 1.

  3. 3
    After 7

    Add 7 to the existing total, giving total 11. Increase the count to 2.

  4. 4
    After 9

    Add 9 to the existing total, giving total 20. Increase the count to 3.

  5. 5
    After the loop

    Divide the final total, 20, by the final count, 3, to compute the average.

Result

The average is 20 divided by 3, approximately 6.67.

What the Running Total Forgets

The running-total method keeps a compact summary rather than the original data. After the inputs 4, 7, and 9 have been processed, the program has total 20 and count 3. It does not retain the individual numbers as separate data in this method. Once the average has been computed, the original entries are no longer available for direct inspection.

Running totaltotal204count3List storage79keepskeepsstoresstoresstores
What information is retained or discarded after each number is entered in each approach?
ApproachRetained after inputStrengthLimitation
Running totalThe sum and the countUses less memory and is simpler when only one final result is neededThe individual numbers are not available for later analysis
List storageEvery entered numberSupports further analysis, comparisons, and debuggingUses more memory than keeping only a sum and count

The approaches produce the same average but preserve different information.

Building and Reading a List

The list approach starts with an empty list before the loop. Each number entered by the user is appended to that list. The list therefore grows as input is collected: after one entry it contains one number, after two entries it contains two numbers, and so on. Unlike the running-total approach, this method preserves the original sequence of values.

4user inputnumbers[4]7user inputnumbers[4, 7]9user inputnumbers[4, 7, 9]appendappendappend
How does each new input move into and change the contents of the list?

After the loop, sum() adds the numbers in the list and returns their total. The len() function counts the elements in the list and returns that count. For a list containing 4, 7, and 9, sum() provides 20 and len() provides 3. Dividing those results produces the same average as the running-total method.

numbers[4, 7, 9]sum()20Average20 divided by 3len()3add elementscount elementscombine with countdivide by
How do sum() and len() extract the total and number of values from the stored list to produce an average?

Choosing What to Preserve

Choose the running-total method when the program needs one final result, such as an average, and does not need the original entries afterward. It is simpler, uses less memory, and runs slightly faster. Choose list storage when the program may need to inspect the original values, debug the collected input, compare entries, or calculate several statistics from the same data.

Worked example

Selecting an approach

A program only needs to display the average once and will not inspect the entered numbers again. Which approach fits better?

  1. 1
    Identify the required result

    The program needs one final result: the average.

  2. 2
    Check whether original data is needed

    The individual inputs will not be used after the average is produced.

  3. 3
    Select the method

    The running-total method fits because it keeps the required sum and count without retaining every number.

Result

Use the running-total method for this requirement. If later requirements change and the program must inspect or reuse the inputs, list storage becomes the more flexible choice.

Mistakes That Distort the Result

  • Common mistake

    Starting the list only after the loop begins

    What it looks like

    The program does not create an empty list before collecting inputs.

    Why it fails

    The list must exist before numbers can be appended to it.

    Do this instead

    Initialize the empty list before the input loop.

  • Common mistake

    Forgetting to convert input strings to numbers

    What it looks like

    The program treats user-entered values as text instead of numeric values.

    Why it fails

    The average requires numerical addition and division.

    Do this instead

    Convert each input string to a number before adding it to the total or storing it in the list.

  • Common mistake

    Dividing by an empty list's length

    What it looks like

    The program attempts to compute an average when no numbers were collected.

    Why it fails

    There is no count of values to divide by when the list is empty.

    Do this instead

    Check that the list is not empty before dividing by its length.

  • Common mistake

    Expecting a running total to answer questions about individual values

    What it looks like

    The program keeps only total and count, then later tries to find the median or identify an outlier.

    Why it fails

    The individual numbers were not retained by the running-total method.

    Do this instead

    Use list storage when later analysis requires the original data.

Practice the Decision

MEDIUM

A program collects several measurements. It must calculate the average now and may later need to find the maximum and minimum measurements. Should it use a running total or a list? Explain what information your choice preserves.

Hints
  • Ask whether the program needs the individual measurements after the average is calculated.
  • Finding additional statistics from the same data requires access to the original values.
EASY

Trace both approaches for the inputs 3, 5, and 8. For the running-total approach, record total and count after each input. For the list approach, record the list after each input. Then identify which values remain available after the loop.

Hints
  • For the running-total approach, update the sum and count once for every input.
  • For the list approach, append each new number to the existing list.

Key Takeaways

  1. 1
    The running-total method tracks only total and count as input arrives.
  2. 2
    The list method appends every number to a list and preserves the original data.
  3. 3
    sum() returns the total of the numbers in a list, while len() returns the number of elements.
  4. 4
    Use a running total for a simple final result when the original values are not needed later.
  5. 5
    Use a list when future analysis, comparisons, debugging, or multiple statistics may require the original values.
  6. 6
    Initialize the list before the loop, convert input strings to numbers, and check that collected data is not empty before dividing.

At a glance

  • A running total stores a sum and a count, while list storage retains every input.
  • Both approaches can compute the same average, but only the list approach preserves the original data for later analysis.
  • The list functions sum() and len() provide the total and number of values needed for an average.
  • Trace state after every input to understand program flow and locate mistakes.
  • Choose the method according to whether the program needs only one result or needs the data afterward.

Related concepts

More concepts starting with L