|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
005课后作业动动手 判断闰年年份的问题 -- 变更
想要让程序有3次输入年份的机会,如果3次中,输入到了闰年,则结束,如果3次都没有输入到闰年年份,也结束。
print("判断闰年")
temp = input('请输入一个年份:')
while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if (year/400) == int(year/400):
print(temp, " 是闰年!")
else:
times = 3
year = int(temp)
while (year/400 != int(year/400)) and (times > 0):
print('请重新试一次吧:')
times = times - 1
[结果是把 “请重新试一次吧 ” 循环了3遍........]
求问大神们,有什么理解,设置次数的窍门么。。。
你没有在 while 循环中更改 temp。
代码帮你改好了:
- print("判断闰年")
- temp = input('请输入一个年份:')
- while not temp.isdigit():
- temp = input("抱歉,您的输入有误,请输入一个整数:")
- year = int(temp)
- if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
- print(temp, "是闰年!")
- else:
- times = 3
- year = int(temp)
- while (times > 0):
- if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
- break
- else:
- year = int(input('请重新试一次吧:'))
- times = times - 1
- if times >= 0:
- print(year, "是闰年!")
- else:
- print("没机会了!")
复制代码
|
|