我获得平均水平的计划并不奏效

时间:2016-11-18 19:08:57

标签: python

def opdracht3()
a = True
result = 0
waslijst = []
while a:
    n = input("Enter a number: ")
    if n == "stop":
        a = False
    else:
        waslijst += n
for nummer in waslijst:
    result += int(nummer)
eind = result / len(waslijst)
print(eind)
opdracht3()

我想获得正在创建的列表的平均值,但是当我添加11之类的数字时,len(waslijst)设置为2而不是1.是否有其他方法可以获得平均值,或者我是使用len函数错误?

1 个答案:

答案 0 :(得分:1)

您需要使用.append方法将所有元素存储在列表中。

def opdracht3():
    a = True
    result = 0
    waslijst = []
    while a:
       n = input("Enter a number: ")
       if n == "stop":
          a = False
       else:
          waslijst.append(n)
    for nummer in waslijst:
       result += int(nummer)
    eind = result / len(waslijst)
    print(eind)
opdracht3()
相关问题