【问题标题】:Why am I getting error. TypeError: unsupported operand type(s) for +: 'int' and 'str'?为什么我会出错。 TypeError:+:'int'和'str'不支持的操作数类型?
【发布时间】:2018-03-31 20:38:16
【问题描述】:

我目前在 python3 中的程序遇到问题。

我最近遇到了错误: 类型错误:+ 的不支持的操作数类型:'int' 和 'str'

我不知道问题可能是什么,因此非常感谢任何帮助。

lengths = input("Enter the Lengths of the Sides of the Shape Seperated by commas: ").split(',')
            answer = sum(lengths)

            print("+".join(lengths) + "= %s" % (answer))

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    “lengths”是一个包含字符串的列表。 “sum”对数字类型进行操作。您需要将字符串转换为浮点数。尝试在 sum 之前添加这一行,并在 lengths_float 上求和:

    lengths_float=[float(element) for element in lengths]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-15
      • 2012-11-29
      • 2012-12-31
      • 1970-01-01
      • 2015-05-29
      • 2021-10-13
      • 1970-01-01
      相关资源
      最近更新 更多