Select Page

Python Programming – John Zelle: Chapter 3 ex. 14

Write a program to find the average of a series of numbers entered by the user.
The program should first prompt the user how many numbers there are.
Note: The average should always be a float, even if the user inputs are all ints

```#Chapter 3 Exercise 14
# Write a program to find the average of a series of numbers entered by the user.
# The program should first prompt the user how many numbers there are.
# Note: The average should always be a float, even if the user inputs are all ints
def main():
print("This program averages a series of numbers entered by the user")
num = eval(input("How many numbers do you want to average? "))
x = 0
s = 0
while x < num:
try:
s += int(input('Enter a number: '))
except ValueError:
print ("Invalid input.")
else:
x += 1
print("The total value of your", num, "numbers is ", s)
print("The average of your", num, "numbers is ", s/num)

main()

```

Run =
This program averages a series of numbers entered by the user
How many numbers do you want to average? 5
Enter a number: 3
Enter a number: 4
Enter a number: 4
Enter a number: 6
Enter a number: 5
The total value of your 5 numbers is 22
The average of your 5 numbers is 4.4