根据用户输入的出生年份,判断并打印出用户是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后')
http://blog.csdn.net/jamiecheung
http://blog.csdn.net/u010841622