Loops and User Input
The running total method maintains only a sum and count, discarding individual numbers after computing the average.
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.
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.
Tracing three inputs
A user enters the numbers 4, 7, and 9. Trace the running-total method.
Before the loop: The method begins with a total of 0 and a count of 0.
After 4: Add 4 to the total, giving total 4. Increase the count to 1.
After 7: Add 7 to the existing total, giving total 11. Increase the count to 2.
After 9: Add 9 to the existing total, giving total 20. Increase the count to 3.
After the loop: Divide the final total, 20, by the final count, 3, to compute the average.
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.
| Approach | Retained after input | Strength | Limitation |
|---|---|---|---|
| Running total | The sum and the count | Uses less memory and is simpler when only one final result is needed | The individual numbers are not available for later analysis |
| List storage | Every entered number | Supports further analysis, comparisons, and debugging | Uses more memory than keeping only a sum and count |
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.
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.
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.
Selecting an approach
A program only needs to display the average once and will not inspect the entered numbers again. Which approach fits better?
Identify the required result: The program needs one final result: the average.
Check whether original data is needed: The individual inputs will not be used after the average is produced.
Select the method: The running-total method fits because it keeps the required sum and count without retaining every number.
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
Starting the list only after the loop begins
The list must exist before numbers can be appended to it.
Fix:
Initialize the empty list before the input loop.Forgetting to convert input strings to numbers
The average requires numerical addition and division.
Fix:
Convert each input string to a number before adding it to the total or storing it in the list.Dividing by an empty list's length
There is no count of values to divide by when the list is empty.
Fix:
Check that the list is not empty before dividing by its length.Expecting a running total to answer questions about individual values
The individual numbers were not retained by the running-total method.
Fix:
Use list storage when later analysis requires the original data.
Practice the Decision
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.
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
- The running-total method tracks only total and count as input arrives.
- The list method appends every number to a list and preserves the original data.
- sum() returns the total of the numbers in a list, while len() returns the number of elements.
- Use a running total for a simple final result when the original values are not needed later.
- Use a list when future analysis, comparisons, debugging, or multiple statistics may require the original values.
- Initialize the list before the loop, convert input strings to numbers, and check that collected data is not empty before dividing.
Key Takeaways
- 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.