代码修改
temp = input('请输入一个年份:')while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
if year/400 == int(year/400):
print(temp + ' 是闰年!')
else:
if (year/4 == int(year/4)) and (year/100 != int(year/100)):
print(temp + ' 是闰年!')
else:
print(temp + ' 不是闰年!')
以上代码写年份算是不是闰年,是能写一次,如何修改成:如果不是闰年还可以继续写,或者多次写,求教 本帖最后由 txxcat 于 2020-4-6 00:37 编辑
加一个while循环,但是要设置好退出循环的条件,参考下面代码:
while True:
print('请输入一个整数(按q退出):',end='')
temp = input()
if temp.lower()=='q':
break
elif not temp.isdigit():
print('抱歉,您的输入有误,',end='')
continue
year = int(temp)
if year/400 == int(year/400):
print(temp + ' 是闰年!')
else:
if (year/4 == int(year/4)) and (year/100 != int(year/100)):
print(temp + ' 是闰年!')
else:
print(temp + ' 不是闰年!') while True:
temp = input('请输入一个年份:')
while not temp.isdigit():
temp = input("抱歉,您的输入有误,请输入一个整数:")
year = int(temp)
# 如果是闰年则退出死循环
if year/400 == int(year/400):
print(temp + ' 是闰年!')
break
else:
if (year/4 == int(year/4)) and (year/100 != int(year/100)):
print(temp + ' 是闰年!')
break
else:
print(temp + ' 不是闰年!请再输入一次!') zltzlt 发表于 2020-4-6 08:05
谢谢!
页:
[1]