gpt4 book ai didi

python - 幻像类型错误(Python 3.3)

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:19 25 4
gpt4 key购买 nike

我试图用一个 dataList 作为 float 列表来运行这段代码:

meanData = round(float((sum(dataList) / len(dataList))), 2)

它给了我一个 TypeError: unsupported operand type(s) for +: 'int' and 'str',即使我什至没有使用 + 运算符。可能是什么问题?

最佳答案

dataList 必须包含一个或多个字符串。此外,sum 不支持字符串。

请看下面的演示:

>>> lst = [1, 2, 3, '4']
>>> sum(lst)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>

要解决此问题,您需要使 dataList 仅包含整数或 float 。

关于python - 幻像类型错误(Python 3.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20482794/

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