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