Apr 16, 2025 | 148 words | 1 min read
3.2.2. Sum Average#
Write a Python program with a loop that asks the user to enter a series of non-negative numbers (positive numbers or zero). The user should enter a negative number to signal the end of the series. After all the non-negative numbers have been entered, the program should display their sum and average.
Sample Output#
Use the values in Table 3.3 below to test your program.
Case |
Numbers |
---|---|
1 |
0, 1, 2, 3, 4, 5, -1 |
2 |
2.496, 11.271, 5.571, -0.01 |
3 |
-10 |
Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.
Case 1 Sample Output
$ python3 sum_average_login.py Enter a non-negative number (negative to quit): 0 Enter a non-negative number (negative to quit): 1 Enter a non-negative number (negative to quit): 2 Enter a non-negative number (negative to quit): 3 Enter a non-negative number (negative to quit): 4 Enter a non-negative number (negative to quit): 5 Enter a non-negative number (negative to quit): -1 You entered 6 numbers. Their sum is 15.000 and their average is 2.500.
Case 2 Sample Output
$ python3 sum_average_login.py Enter a non-negative number (negative to quit): 2.496 Enter a non-negative number (negative to quit): 11.271 Enter a non-negative number (negative to quit): 5.571 Enter a non-negative number (negative to quit): -0.01 You entered 3 numbers. Their sum is 19.338 and their average is 6.446.
Case 3 Sample Output
$ python3 sum_average_login.py Enter a non-negative number (negative to quit): -10 You didn't enter any numbers.
Deliverables#
Save your finished program as sum_average_login.py
, replacing
login
with your Purdue login. Then submit it along with all the
deliverables listed in
Table 3.4 below.
Deliverable |
Description |
---|---|
|
Your finished program. |
Screenshot(s) |
PNG(s) capturing all 3 test cases. |