How do I fix TypeError: unsupported operand type(s) for +: 'int' and 'str'
How do I fix TypeError: unsupported operand type(s) for +: 'int' and 'str'
我正在编写计算器,当我 运行 程序出现此错误消息时:
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))
试着用逗号代替 +
:
print x , "+" , y , "=" , mathResult , "."
在python3中:print(x , "+" , y , "=" , mathResult , ".")
如果你在 python 3.6.
print(f"{x} + {y} = {mathResult}.")
我正在编写计算器,当我 运行 程序出现此错误消息时:
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))
试着用逗号代替 +
:
print x , "+" , y , "=" , mathResult , "."
在python3中:print(x , "+" , y , "=" , mathResult , ".")
如果你在 python 3.6.
print(f"{x} + {y} = {mathResult}.")