Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
跳过Python while循环_Python - Fatal编程技术网

跳过Python while循环

跳过Python while循环,python,Python,我试图制作一个倒计时时钟,当我运行它时,最后一个while循环被跳过,循环完全退出。有什么问题吗 def countdown(): print("Give the time for the countdown separated by a space.") time.sleep(0.3) print("If none just type in 00") hours_time = int(input("Hours:&

我试图制作一个倒计时时钟,当我运行它时,最后一个while循环被跳过,循环完全退出。有什么问题吗

def countdown():
    print("Give the time for the countdown separated by a space.")
    time.sleep(0.3)
    print("If none just type in 00")
    hours_time = int(input("Hours:"))
    min_time = int(input("Minutes:"))
    sec_time = int(input("Seconds:"))
    print("Countdown will run for:{}Hr {}min {}sec".format(hours_time, min_time, sec_time))
    while (sec_time != 00):
        print("{}Hr {}min {}sec".format(hours_time, min_time, sec_time))
        time.sleep(1)
        sec_time = sec_time - 1
        while (min_time != 00 and sec_time == 00):
            print("{}Hr {}min 00sec".format(hours_time, min_time))
            time.sleep(1)
            min_time = min_time - 1
            sec_time = sec_time + 59
            while (hours_time != 00 and min_time == 00 and sec_time == 00):
                print("{}Hr 00min 00sec".format(hours_time))
                time.sleep(1)
                hours_time = hours_time - 1
                min_time = min_time + 59
                sec_time = sec_time + 59

countdown()

您只需要一个
,而
循环。每次将秒数减少1,如果秒数低于零,则相应调整分钟数,与小时数类似:

    while sec_time != 0 or min_time != 0 or hours_time != 0:
        print("{}Hr {}min {}sec".format(hours_time, min_time, sec_time))
        time.sleep(1)
        sec_time -= 1
        if sec_time < 0:
            sec_time += 60
            min_time -= 1
            if min_time < 0:
                min_time += 60
                hours_time -= 1
而秒时间!=0或分钟时间!=0或小时\u时间!=0:
打印(“{}Hr{}min{}sec.”格式(小时时间、分钟时间、秒时间))
时间。睡眠(1)
秒时间-=1
如果秒时间<0:
秒时间+=60
最小时间-=1
如果最小时间<0:
最小时间+=60
小时/u时间-=1

循环时只需要一个
。每次将秒数减少1,如果秒数低于零,则相应调整分钟数,与小时数类似:

    while sec_time != 0 or min_time != 0 or hours_time != 0:
        print("{}Hr {}min {}sec".format(hours_time, min_time, sec_time))
        time.sleep(1)
        sec_time -= 1
        if sec_time < 0:
            sec_time += 60
            min_time -= 1
            if min_time < 0:
                min_time += 60
                hours_time -= 1
而秒时间!=0或分钟时间!=0或小时\u时间!=0:
打印(“{}Hr{}min{}sec.”格式(小时时间、分钟时间、秒时间))
时间。睡眠(1)
秒时间-=1
如果秒时间<0:
秒时间+=60
最小时间-=1
如果最小时间<0:
最小时间+=60
小时/u时间-=1

这使用格式字符串使时间值具有前导零:

seconds = (hours_time*60 + min_time)*60 + sec_time
while seconds != 0:
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    print("{0:02d}Hr {1:02d}min {2:02d}sec".format(h,m,s))
    time.sleep(1)
    seconds = seconds - 1

这将使用格式字符串使时间值具有前导零:

seconds = (hours_time*60 + min_time)*60 + sec_time
while seconds != 0:
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    print("{0:02d}Hr {1:02d}min {2:02d}sec".format(h,m,s))
    time.sleep(1)
    seconds = seconds - 1

sec_time==00
在最里面的循环中不能为真。包含循环已检查
sec\u time==00
,然后执行
sec\u time=sec\u time+59
——这意味着
sec\u time
在输入最内层循环之前是
59
sec\u time==00
在最内层循环中不能为真。包含循环已检查
sec\u time==00
,然后执行
sec\u time=sec\u time+59
——这意味着
sec\u time
在进入最里面的循环之前是
59