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.9231 Voting
Lütfi
Usta · 5 yıl öğretmeniUzman doğrulaması
Cevap
Here's a Python program that meets the requirements:<br />```python<br /># initialize variables<br />total = 0<br />count = 0<br /><br /># loop to get input from user<br />while True:<br /> num = float(input("Enter numbers to sum up (to stop enter 0): "))<br /> if num == 0:<br /> break<br /> total += num<br /> count += 1<br /><br /># calculate average<br />average = total / count<br /><br /># print result<br />print("Average of numbers:", average)<br />```<br />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.
Derecelendirmek için tıklayın: