python里面求平均数

如题所述

arr=[]
while True:
    tmp=input('enter a number>>')
    i=int(tmp)
    if i>=0:
        arr.append(i)
    else:
        break
l=len(arr)
y=0
for i in range(0,l):
    y=y+arr[i]
print 'the average of the numbers is ',1.0*y/l

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-07
list1 = []
a = 1
while True:
    if int(a) < 0:
        break
    else:
        a = int(raw_input("Enter a number(<negative to quit>)"))
        list1.append(a)
list2 = list1[:-1]
average = float(sum(list2)) / len(list2)
print ("The average of the numbers is %s" % average)

相似回答