Soru
Part 1 (30pts): Write Python statements that get unknown number of positive numbers and calculate their mee (average). You need two variables, one to store the total and one to store the number of number Stop your program when user enters 0. Part 1: Enter numbers to sum up(to stop enter 0): 10 Enter numbers to sum up(to stop enter 0): 20.8 Enter numbers to sum up(to stop enter 0): 12 Enter numbers to sum up(to stop enter 0): 19.7 Enter numbers to sum up(to stop enter 0):0 Average of numbers : 15.625
Çözüm
3.9
(231 Oylar)
Lütfi
Usta · 5 yıl öğretmeni
Uzman doğrulaması
Cevap
Here's a Python program that meets the requirements:```python# initialize variablestotal = 0count = 0# loop to get input from userwhile True: num = float(input("Enter numbers to sum up (to stop enter 0): ")) if num == 0: break total += num count += 1# calculate averageaverage = total / count# print resultprint("Average of numbers:", average)```This program uses a `while` loop to repeatedly ask the user for input until they enter `0`. The program keeps track of the total sum and the number of inputs using the `total` and `count` variables. When the user enters `0`, the program breaks out of the loop and calculates the average by dividing the total by the count. Finally, the program prints the average.