鱼C论坛

 找回密码
 立即注册
查看: 2017|回复: 6

[已解决]循环次数的控制

[复制链接]
发表于 2020-2-8 02:39:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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遍........]
求问大神们,有什么理解,设置次数的窍门么。。。
最佳答案
2020-2-8 08:56:40
你没有在 while 循环中更改 temp。

代码帮你改好了:

  1. print("判断闰年")
  2. temp = input('请输入一个年份:')
  3. while not temp.isdigit():
  4.     temp = input("抱歉,您的输入有误,请输入一个整数:")

  5. year = int(temp)
  6. if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
  7.     print(temp, "是闰年!")
  8. else:
  9.     times = 3
  10.     year = int(temp)
  11.     while (times > 0):
  12.         if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
  13.             break
  14.         else:
  15.             year = int(input('请重新试一次吧:'))
  16.             times = times - 1
  17.     if times >= 0:
  18.         print(year, "是闰年!")
  19.     else:
  20.         print("没机会了!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-8 07:49:31 From FishC Mobile | 显示全部楼层
python的/得到的都是浮点数
python的//才是整除的意思
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-8 08:14:03 | 显示全部楼层
不是print,是input
要的是接收用户输入
  1. temp = input('请重新试一次吧:')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-8 08:56:40 | 显示全部楼层    本楼为最佳答案   
你没有在 while 循环中更改 temp。

代码帮你改好了:

  1. print("判断闰年")
  2. temp = input('请输入一个年份:')
  3. while not temp.isdigit():
  4.     temp = input("抱歉,您的输入有误,请输入一个整数:")

  5. year = int(temp)
  6. if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
  7.     print(temp, "是闰年!")
  8. else:
  9.     times = 3
  10.     year = int(temp)
  11.     while (times > 0):
  12.         if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
  13.             break
  14.         else:
  15.             year = int(input('请重新试一次吧:'))
  16.             times = times - 1
  17.     if times >= 0:
  18.         print(year, "是闰年!")
  19.     else:
  20.         print("没机会了!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-8 13:40:44 | 显示全部楼层
zltzlt 发表于 2020-2-8 08:56
你没有在 while 循环中更改 temp。

代码帮你改好了:

感恩的心!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-8 18:21:55 | 显示全部楼层
zltzlt 发表于 2020-2-8 08:56
你没有在 while 循环中更改 temp。

代码帮你改好了:

好像出现bug了;
输入3次之后,第4次无论输入什么,都是闰年。
【如下】
判断闰年
请输入一个年份:2019
请重新试一次吧:2017
请重新试一次吧:1033
请重新试一次吧:1041
1041 是闰年!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-8 18:31:03 | 显示全部楼层
看看大佬的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-3 21:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表