python的強制轉換(當出現 not supported between instances of 'str' and 'int' 的錯誤時)


當我們編程時,有時會出現如下錯誤:TypeError: '>' not supported between instances of 'str' and 'int'

如下圖:


 

 這是因為input()返回的數據類型是str類型,不能直接和整數進行比較,必須先把str轉換成整型,使用int()方法:age = int(input ("請輸入你的年齡:"))

 

 

 改正之后為:

 

 這樣程序就達到了預期的效果了


免責聲明!

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



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