鱼C论坛

 找回密码
 立即注册
查看: 2176|回复: 2

对于while循环的疑问。。

[复制链接]
发表于 2017-2-1 21:35:07 | 显示全部楼层 |阅读模式

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

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

x
temp = 1
key = 1
temp = input("please enter the uncertain year : \n")
while key != 0:
    while not temp.isdigit():
        print("Input Error!")
        temp = input("please enter again")
    year = int(temp)
    k = int(year%4)
    t = int(year%400)
    if k == 0:
        if year%100!=0:
            print("yea dat is a leap year!")
        else:
            if t == 0:
                print("yea dat is a leap year!")
            else:
                print("but sorry ,dat is not a leap year!")
    key = input("if you wanna countinue please enter the year,if not enter 0 \n")
    temp = key

就像上面,进行一次判别后,如果不想要继续程序运行了,输入0,但每一次输入0似乎程序都把0当作需要判别的年份又进行了一次判别

请问这个问题如何解决啊?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-2 08:37:17 | 显示全部楼层
试试 break
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-2 14:38:53 | 显示全部楼层
temp = 1
key = 1
temp = input("please enter the uncertain year : \n")
while key != 0:
    while not temp.isdigit():
        print("Input Error!")
        temp = input("please enter again")
    year = int(temp)
    k = int(year%4)
    t = int(year%400)
    if k == 0:
        if year%100!=0:
            print("yea dat is a leap year!")
        else:
            if t == 0:
                print("yea dat is a leap year!")
            else:
                print("but sorry ,dat is not a leap year!")
    else :
        print("but sorry ,dat is not a leap year!")

    #这里应该再加一个else,否则不是4的倍数就不会输出判断的结果而是直接让你再输入
    key = int(input("if you wanna countinue please enter the year,if not enter 0 \n"))
    #把key给int掉,然后再去判断,否则的话会一直循环,即使你输的是0


    temp = str(key)
    #把key给int掉temp就是int型,不包含isdigit()方法,所以给str回来
   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 12:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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