您的位置:首页 > 编程语言 > Python开发

Python---TypeError: '<' not supported between instances of 'str' and 'int'

2017-02-09 14:06 826 查看
根据用户输入的出生年份,判断并打印出用户是00后还是00前

birth = input('birth:')
if birth < 2000:
print('00前')
else:
print('00后')


此时如果直接输入1988,报错如下:

Traceback (most recent call last):

  File "z.py", line 2, in <module>

    if birth < 2000:

TypeError: '<' not supported between instances of 'str' and 'int'


这是因为input()返回的数据类型是str,不能直接和整数进行比较,必须先把str换成整数,使用int()方法:

s = input('birth:')
birth = int(s)
if birth < 2000:
print('00前')
else:
print('00后')



更多iOSAndroidPython的文章,请点击:
http://blog.csdn.net/jamiecheung


更多JavaUnity3D的文章,请点击:
http://blog.csdn.net/u010841622
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python
相关文章推荐