如何修复 TypeError: +: 'int' 和 '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))


当年话下
浏览 142回答 3
3回答

慕田峪4524236

在python3中:print(x , "+" , y , "=" , mathResult , ".")

繁星coding

试着用逗号代替+:print&nbsp;x&nbsp;,&nbsp;"+"&nbsp;,&nbsp;y&nbsp;,&nbsp;"="&nbsp;,&nbsp;mathResult&nbsp;,&nbsp;"."

森林海

如果您使用的是 python 3.6。print(f"{x}&nbsp;+&nbsp;{y}&nbsp;=&nbsp;{mathResult}.")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python