我如何修复TypeError:+不支持的操作数类型:'int'和'str'

问题描述 投票:-2回答:3

我正在编码计算器,当我运行程序时,出现此错误消息:

Traceback (most recent call last):
  File "/Users/sergioley-languren/Documents/itt/independent_projects/Mathematical_Calculator.py", line 66, in <module>
    print(x + "+" + y + "=" + mathResult + ".") ; sleep(float(speed))
TypeError: unsupported operand type(s) for +: 'int' and 'str'

这是出现错误的代码:

print("type in your addition problem with your x variable first. (x variable = your first number) CAUTION: This calculator only supports 2 numbers at the moment.") ; sleep(float(speed))
        x = int(input())
        print("Type in your y variable.") ; sleep(float(speed))
        y = int(input())
        mathResult = x + y
        print(x + "+" + y + "=" + mathResult + ".") ; sleep(float(speed))
python typeerror calculator addition
3个回答
2
投票
在python3中:print(x , "+" , y , "=" , mathResult , ".")

1
投票
只需尝试用逗号代替+

print x , "+" , y , "=" , mathResult , "."


0
投票
如果您使用的是python 3.6。

print(f"{x} + {y} = {mathResult}.")

© www.soinside.com 2019 - 2024. All rights reserved.