原文鏈接:http://www.juzicode.com/archives/2411
錯誤提示:
進行加減法運算時提示:TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’:
D:\juzicode>python test.py
Traceback (most recent call last):
File "test.py", line 6, in <module>
c=a+b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
可能原因:
1、不同類型的變量相加減。
解決方法:
1、無解,如果a,b變量類型分別為int和str,無法直接進行加減運算。本意可能是要進行類型轉換后再加減,比如可以先把str類型的’3’轉換為int類型再加減。
a=5
b='3'
c=a+int(b)
print(c)