gpt4 book ai didi

python - 如何在 Python 中对列表使用 sum() 函数?

转载 作者:太空狗 更新时间:2023-10-29 20:34:09 25 4
gpt4 key购买 nike

我正在做作业,它要求我使用 sum() 和 len() 函数来查找输入数字列表的平均值,当我尝试使用 sum() 来获取列表的总和时,我收到错误类型错误:+ 不支持的操作数类型:“int”和“str”。以下是我的代码:

numlist = input("Enter a list of number separated by commas: ")

numlist = numlist.split(",")

s = sum(numlist)
l = len(numlist)
m = float(s/l)
print("mean:",m)

最佳答案

问题是当你从输入中读取时,你有一个字符串列表。你可以在第二行做类似的事情:

numlist = [float(x) for x in numlist]

关于python - 如何在 Python 中对列表使用 sum() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9746522/

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