输入错误 怎样写循环返回重新输入?
year=0num=3
while year<2018 and num>0:
year = int(input('请输入年份:'))
num=num-1
if 0 <= year <= 2018:
if year % 4:
print('不是闰年')
else:
if year % 400:
if year % 100:
print("闰年")
else:
print('不是闰年')
else:
print('闰年')
if num == 1:
print('最后一次,',end=' ')
else:
print("输入错误")
“输入错误”后怎样 重新坐在一个循环 重新输入运行上面程序?请大神们解惑…… 本帖最后由 凌九霄 于 2018-12-10 21:54 编辑
else:
print("输入错误")
continue 本帖最后由 heidern0612 于 2018-12-11 08:47 编辑
1、把你while条件换宽一些,换成while True。
(话说你这个条件year<2018 and num >0 限制的完全没用啊,year不能超过2018,num本来也是大于0)
2、如果想设置次数,把最后的if num 改成 == 0,break掉循环就可以了。 heidern0612 发表于 2018-12-11 08:39
1、把你while条件换宽一些,换成while True。
(话说你这个条件year0 限制的完全没 ...
感谢!放宽一些 在限制次数。ok了,再次感谢 本帖最后由 loster0219 于 2018-12-11 12:15 编辑
num = 2 #从0开始,定义3的话,实际是4次
year = input('请输入年份:')
while num > 0:
while not year.isdigit():# 输入的不是数字
year = input("输入错误,请重新输入:")
else:# 输入的是数字,开始循环
num -= 1
if (int(year) % 4 == 0 and int(year) % 100 != 0) or (int(year) % 400 == 0):# 判断闰年
print("是闰年,还有",num+1,"次") #num+1 人性化输出一点,不然会出现提示还有0次
year = input("请继续输入:")
else:
print("不是闰年,还有",num+1,"次")
year = input("请继续输入:")
按照你的想法,我写的是这样的,如果输入的数据类型不是数字,也就是输入错误,无限重新输入,不会退出循环
输入年份数字的话,就是3次机会.
不知道是不是你想要的效果
页:
[1]