本文主要是介绍Python报错集合篇3-TypeError: unsupported operand type(s) for +: 'int' and 'str',希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文介绍如何处理报错–TypeError: unsupported operand type(s) for +: ‘int’ and 'str’
源代码:
print(1 + '1')
运行报错如下:
Traceback (most recent call last):File "D:/Program Files/PycharmProjects/pycharmPrj/zzz/Python/lx0.py", line 48, in <module>print(1 + '1')
TypeError: unsupported operand type(s) for +: 'int' and 'str'
由于1是‘int’型,“1”是‘str’型,‘int’和‘str’型不可相加,除非两个都是‘int’型或‘str’型,才可相加
修改如下:
print(1 + int(‘1’))
print(str(1) + ‘1’)
C:\Python37\python.exe "D:/Program Files/PycharmProjects/pycharmPrj/zzz/Python/lx0.py"
2
‘11’
Process finished with exit code 0
这篇关于Python报错集合篇3-TypeError: unsupported operand type(s) for +: 'int' and 'str'的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!