Countdown Clock: 01:05

后端 未结 5 2136
长情又很酷
长情又很酷 2020-12-13 07:31

How can I create a countdown clock in Python that looks like 00:00 (min & sec) which is on a line of its own. Every time it decreases by one actual second then

5条回答
  •  自闭症患者
    2020-12-13 07:44

    Here is the code which counts from 01:05 to 00:00 in MM:SS format.

    Python 3 :

    import time
    def countdown(p,q):
        i=p
        j=q
        k=0
        while True:
            if(j==-1):
                j=59
                i -=1
            if(j > 9):  
                print(str(k)+str(i)+":"+str(j), end="\r")
            else:
                print(str(k)+str(i)+":"+str(k)+str(j), end="\r")
            time.sleep(1)
            j -= 1
            if(i==0 and j==-1):
                break
        if(i==0 and j==-1):
            print("Goodbye!", end="\r")
            time.sleep(1)
    countdown(1,5) #countdown(min,sec)
    

    Python 2 :

    import time
    def countdown(p,q):
        i=p
        j=q
        k=0
        while True:
            if(j==-1):
                j=59
                i -=1
            if(j > 9):  
                print "\r"+str(k)+str(i)+":"+str(j),
            else:
                print "\r"+str(k)+str(i)+":"+str(k)+str(j),
            time.sleep(1)
            j -= 1
            if(i==0 and j==-1):
                break
        if(i==0 and j==-1):
            print "\rGoodbye!"
            time.sleep(1)
    countdown(1,5) #countdown(min,sec)
    

提交回复
热议问题