Reputation: 1
I'm trying to make a counter that ticks down from 1,000,000 to 0 in increments of 100,000 per second and displays this on one line, updating each second.
However, the below code prints an extra zero at the end:
counter = 1000000
while counter > 0:
sys.stdout.write("%s\r" % counter)
sys.stdout.flush()
counter -= 100000
time.sleep(1)
I get the output (each writing over the previous line):
1000000
9000000
8000000
7000000
6000000
...
The script stops correctly at the end. When I replace \r
with \n
it prints the numbers correctly, but obviously I want it to refresh rather than create a new line each time.
Upvotes: 0
Views: 745
Reputation: 30453
Last character is left from the first line, as a workaround you could overwrite it manually:
while counter > 0:
sys.stdout.write(str(counter).ljust(7) + "\r")
counter -= 100000
time.sleep(1)
Upvotes: 2