简体   繁体   English

typeerror 不支持 / 'str' 和 'int' 的操作数类型

[英]typeerror unsupported operand type(s) for / 'str' and 'int'

I'm trying to find the average score from a list if three numbers, as shown below:如果三个数字,我试图从列表中找到平均分数,如下所示:

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

Then it says: typeerror unsupported operand type(s) for / 'str' and 'int'然后它说: typeerror unsupported operand type(s) for / 'str' and 'int'

I suspect that the error message as you describe may not be accurate for this code, but nonetheless, the issue is that you are trying to treat a list of strings as a list of ints.我怀疑您描述的错误消息对于此代码可能不准确,但问题是您试图将字符串列表视为整数列表。 for example例如

>>> 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

If you want to treat them as ints (or floats) then you will need to add the conversion as in如果要将它们视为整数(或浮点数),则需要将转换添加为

a = [int(n) for n in s.split()]
a = [float(n) for n in s.split()] # alternatively

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError:unsupported operand type(s) for -: 'str' and 'int' + 不支持的操作数类型:“int”和“str”[TypeError] - unsupported operand type(s) for +: 'int' and 'str' [TypeError] TypeError: 不支持的操作数类型 -: 'str' 和 'int' - TypeError: unsupported operand type(s) for -: 'str' and 'int' 类型错误:-= 不支持的操作数类型:'int' 和 'str' - TypeError: unsupported operand type(s) for -=: 'int' and 'str' 类型错误:&lt;&lt;:&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持 - TypeError: unsupported operand type(s) for <<: 'str' and 'int' TypeError:-:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'int' and 'str' 类型错误:不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; (2) - TypeError: unsupported operand type(s) for /: 'str' and 'int' (2) TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'str' and 'int' TypeError:+ =不支持的操作数类型:“ int”和“ str” - TypeError: unsupported operand type(s) for +=: 'int' and 'str' TypeError:|:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for |: 'int' and 'str'
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM