2

I need to make a timer, that replaces the output. For example:

5

This then replaces with 4 after one second and so on.

This no longer works:

I have made a timer already as follows, but it doesn't do what I expected:

from time import sleep
for i in range(4, -1, -1):
    print(i, end=" ")
    sleep(1)

The actual results are:

4 3 2 1 0

I'm using maCOS.

XneroBot
  • 23
  • 5

2 Answers2

2

What about using the DEL ASCII character?

import time


for i in range(4, -1,-1):
    s = str(i)
    print(i, end='', flush=True)
    time.sleep(1)
    print('\u0008' * len(s), end='', flush=True)
print()
norok2
  • 25,683
  • 4
  • 73
  • 99
0

For Mac or Linux you can use print('\033c', end='') to clear the terminal. So your code should look like:

from time import sleep:

for i in range(4, -1,-1):
    print(i)
    print('\033c', end='')
    sleep(1)         
Abhyudai
  • 826
  • 7
  • 16