|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
print('输入一个年份:',end=' ')
temp = input()
while not isinstance(temp,int):
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if (year % 4 == 0) and (year % 100 != 0):
print(temp + "这是闰年")
elif year % 400 == 0:
print(temp + "这是闰年")
else:
print("这不是闰年")
不管输入什么都是重复while循环语句
本帖最后由 Twilight6 于 2020-6-8 16:35 编辑
因为 input 返回的是字符串类型 不能通过isinstance方法来判断是否是整型
用字符串方法 isdigit() 若字符串只包含数字 返回 True 否则 返回 False
- print('输入一个年份:',end=' ')
- temp = input()
- while not temp.isdigit():
- temp = input("抱歉,您的输入有误,请输入一个整数:")
- year = int(temp)
- if (year % 4 == 0) and (year % 100 != 0):
- print(temp + "这是闰年")
- elif year % 400 == 0:
- print(temp + "这是闰年")
- else:
- print("这不是闰年")
复制代码
|
|