0

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

1
  • That code works for me, i.e. prints the time. How do you run it? Commented Aug 3, 2022 at 10:43

1 Answer 1

3
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="")
Sign up to request clarification or add additional context in comments.

1 Comment

Carriage return doesn't wipe printed text (at least for me on macOS, python3.9). UPD: but that might be an issue for OP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.