[英]Why am I getting error. TypeError: unsupported operand type(s) for +: 'int' and 'str'?
I am currently having trouble with a program in python3. 我目前在python3中遇到问题。
I have recently come across the error: TypeError: unsupported operand type(s) for +: 'int' and 'str' 我最近遇到了错误:TypeError:+不支持的操作数类型:“ int”和“ str”
I have no idea what the problem could be so any help would be much appreciated. 我不知道可能是什么问题,因此不胜感激。
lengths = input("Enter the Lengths of the Sides of the Shape Seperated by commas: ").split(',')
answer = sum(lengths)
print("+".join(lengths) + "= %s" % (answer))
"lengths" is a list containing strings. “长度”是一个包含字符串的列表。 "sum" operates on numeric types.
“ sum”对数字类型进行运算。 You need to convert strings to floats.
您需要将字符串转换为浮点数。 Try to add this line before sum, and sum on lengths_float:
尝试在总和之前添加此行,并在lengths_float上总和:
lengths_float=[float(element) for element in lengths]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.