类型错误:不支持使用'str'和'int'作为除数的操作数类型

2024-09-27 07:31:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个列表中找出三个数字的平均得分,如下所示:

a = (lines.split(":")[1].split(",")[-3:])
Print (a)
Averagescore = sum(a)/len(a)
Print (averagescore)

然后它说: typeerror不支持/“str”和“int”的操作数类型


Tags: 类型列表len数字intsplitsumlines
1条回答
网友
1楼 · 发布于 2024-09-27 07:31:01

我怀疑您所描述的错误消息对于这段代码可能不准确,但问题是您试图将字符串列表视为int列表。例如

>>> s = "1 16 32" # string
>>> s.split() # this returns a list of strings
['1', '16', '32']
>>> s.split()[0] + 1 # you can't add an int to a string
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

如果要将它们视为int(或float),则需要将转换添加为

^{pr2}$

相关问题 更多 >

    热门问题