Python錯誤集錦:TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’


原文鏈接: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'

python-error-unsupport-operator

可能原因:

1、不同類型的變量相加減。

解決方法:

1、無解,如果a,b變量類型分別為int和str,無法直接進行加減運算。本意可能是要進行類型轉換后再加減,比如可以先把str類型的’3’轉換為int類型再加減。

a=5
b='3'
c=a+int(b)
print(c)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM