I am having trouble implementing a simple countdown in python using carriage return. I have two versions, each of which has problems.
Print version:
for i in range(10): print "\rCountdown: %d" % i time.sleep(1)
Problem: \r does nothing, because a newline is printed at the end, so it displays the result:
Countdown: 0 Countdown: 1 Countdown: 2 Countdown: 3 Countdown: 4 Countdown: 5 Countdown: 6 Countdown: 7 Countdown: 8 Countdown: 9
Sys.stdout.write Version:
for i in range(10): sys.stdout.write("\rCountdown: %d" % i) time.sleep(1) print "\n"
Problem: All sleep occurs at the beginning, and after 10 seconds of sleep, it simply prints Countdown: 9 on the screen. I see that \r works behind the scenes, but how can I get fingerprints to be inserted in a dream?
Kvass source share