【问题标题】:digital Countdown from a set time in pythonpython中设定时间的数字倒计时
【发布时间】:2016-12-14 13:54:29
【问题描述】:

我需要创建一个程序,从设定的时间开始进行数字倒计时。它需要被打印出来,所以它显示为 Hours:minutes:seconds。

import time 
count=int(input("Enter your start point")) 
count2=int(input("Enter your start point")) 
count3=int(input("Enter your start point")) 
while count and count and count3 >0: 
    time.sleep(1)   
    print(count,+":",+":",+count3) 
    count -=1 
    count2 -=1 
    count3 -=1

【问题讨论】:

  • 请分享您的尝试...
  • 导入时间 count=int(input("输入你的起点")) count2=int(input("输入你的起点")) count3=int(input("输入你的起点" )) while count and count and count3 >0: time.sleep(1) print(count,+":",+":",+count3) count -=1 count2 -=1 count3 -=1
  • 请用您尝试过的代码更新您的问题。这样你就不会收到不喜欢。

标签: python time


【解决方案1】:
import time


hours = 1
minutes = 0
seconds = 4

while not hours==minutes==seconds==0:
    print str(hours)+":"+str(minutes)+":"+str(seconds)
    time.sleep(1)
    if minutes==seconds==0:
        hours-=1
        minutes=59
        seconds=59
    elif seconds==0:
        minutes-=1
        seconds=59
    else:
        seconds-=1

试试这个。如果需要,可以为数字添加填充。这是 POC。

>>1:0:4
>>1:0:3
>>1:0:2
>>1:0:1
>>1:0:0
>>0:59:59
>>0:59:58
>>0:59:57
>>0:59:56

【讨论】:

    【解决方案2】:

    我修改了你的代码,这将产生你需要的输出。
    如有任何疑问,请告诉我,

    import time 
    count=int(input("Enter your start point")) 
    count2=int(input("Enter your start point")) 
    count3=int(input("Enter your start point")) 
    
    while count | count2 | count3 >0: 
        while(count>=0):
            while(count2>=0):
                while(count3>=0):
                    time.sleep(1) 
                    print count,":",count2,":",+count3
                    count3 -= 1
                count3 = 59
                count2 -= 1
            count2 = 59
            count -= 1 
    

    在您的旧代码中使用了 AND 运算符,因此即使任何变量为零,它也会终止执行。

    输出:

    1 : 0 : 4
    1 : 0 : 3
    1 : 0 : 2
    1 : 0 : 1
    1 : 0 : 0
    0 : 59 : 59
    0 : 59 : 58
    0 : 59 : 57
    0 : 59 : 56
    0 : 59 : 55
    0 : 59 : 54
    0 : 59 : 53
    0 : 59 : 52
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      相关资源
      最近更新 更多