|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- times = 3
- temp = input('请输入一个年份')
- while not temp.isdigit():
- print('抱歉输入错误')
- if times > 1:
- print('请输入整数')
-
- 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('不是闰年')
- if times > 1:
- print('请再输入一遍',end='')
- else:
- print('抱歉机会用光了')
-
- times = times - 1
复制代码
请大佬看看这个自己改的代码,想弄成次数,但是运行起来,一次就结束了,不知道哪里出啦问题
写的可能太粗糙,各位不要嫌弃,给指正一下
您好!从您的描述来看,您希望建立一个具有重试次数的程序,用于判断输入的年份是否为闰年。根据您提供的代码,我发现了一些问题,并对其进行了相应的修改。请参考以下代码:
- times = 3
- while times > 0:
- temp = input('请输入一个年份:')
- if not temp.isdigit():
- print('抱歉,输入错误,请输入整数。')
- else:
- year = int(temp)
- if (year % 400) == 0 or (year % 4 == 0 and year % 100 != 0):
- print(temp + '是闰年')
- else:
- print(temp + '不是闰年')
- times -= 1
- if times > 0:
- print('您还有{}次尝试机会。'.format(times))
- else:
- print('抱歉,机会已用光。')
复制代码
在这个修改后的代码中,我将主要逻辑放入了一个 while 循环中,以便于实现重试次数的功能。在每次循环中,都会提示用户输入年份并进行判断,循环结束后,会根据剩余次数给出相应提示。
|
|