反复要求用户输入数字的程序,只有当用户输入0时才停止


#This is the code I have so far

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#反复编写一个程序,要求用户输入一个数字,只有当他们停止输入0。然后计算并显示所输入数字的平均值。#对于这个程序,我们将使用while-True-else循环

i = input('Type in any number: __')
total = 0           # accumulator
num = 0             # this will serve as the total number of entries
# We also have to store the quantity of numbers the user has entered
# Maybe include a for loop to count how many times the program was run.
while i != 0:
print(i)
total = int(total) + int(i)
answer = input('Would you like to add a number? ')   # After updating the total, 
# ask user if they would like to add number.
if answer == 'y':
#PROBLEM AREA. Tried continue but only breaks the loop  
else:
print('i=0, program is over!')
print('The total amount: ', total) 

num = num + 1
else:
print('Program does not run with 0 values')          
print(total, number, float(total/num))      

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

nums = []
while True:
try:
num = int(input("Input an integer: "))
except ValueError:
num = int(input("Input an integer: "))

nums.append(num)
if num==0:
break
avg = sum(nums)/(len(nums)-1)

相关内容

  • 没有找到相关文章

最新更新