根据用户输入的出生年份,判断并打印出用户是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.youkuaiyun.com/jamiecheung
http://blog.youkuaiyun.com/u010841622
本文介绍了一个简单的Python程序,该程序通过输入用户的出生年份来判断其是否为00后。文章还解释了如何正确处理输入数据类型的问题。
1万+

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



