gpt4 book ai didi

python - 我的求平均值的程序不太有效

转载 作者:太空宇宙 更新时间:2023-11-03 15:57:08 26 4
gpt4 key购买 nike

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函数使用错误?

最佳答案

您需要使用.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()

关于python - 我的求平均值的程序不太有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40684362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com