当我们编程时,有时会出现如下错误:TypeError: '>' not supported between instances of 'str' and 'int'
如下图:

这是因为input()返回的数据类型是str类型,不能直接和整数进行比较,必须先把str转换成整型,使用int()方法:age = int(input ("请输入你的年龄:"))
改正之后为:

这样程序就达到了预期的效果了
本文介绍了一个常见的编程错误:TypeError, 即尝试在字符串和整数间进行比较操作。文章通过一个具体的例子解释了如何使用int()函数将输入的字符串转换为整数,从而避免这一错误。
当我们编程时,有时会出现如下错误:TypeError: '>' not supported between instances of 'str' and 'int'
如下图:

这是因为input()返回的数据类型是str类型,不能直接和整数进行比较,必须先把str转换成整型,使用int()方法:age = int(input ("请输入你的年龄:"))
改正之后为:

这样程序就达到了预期的效果了
转载于:https://www.cnblogs.com/xisheng/p/7531862.html
694
1167
2万+

被折叠的 条评论
为什么被折叠?