Search code examples
pythontimer

Timer implementation in python


I'm trying to implement a timer that counting down hours, mins and secs. I saw a similar implementation on the internet but still yet, there is nothing that printed to the terminal:


import time

time_to_wait = 30

while time_to_wait:
   seconds = time_to_wait % 60
   mins = time_to_wait // 60
   hours = mins * 60
   timer = '{:02d}:{:02d}:{:02d}'.format(hours, mins, seconds)
   print(timer, end="\r")
   time.sleep(1)
   time_to_wait -= 1


Solution

  • print(timer, end="\r")
    

    \r denotes carriage return so timer is printed and then (very quickly) wiped, to avoid that you should print carriage return first, that is please try using

    print("\r", timer, end="")