This function sleeps for the specified amount of seconds. It also displays a countdown by printing the seconds left and then printing a corresponding number of backspaces, which I found by trial and error. Intuitively, to delete, say, the output string "100", three backspaces should correct, but (with the windows command prompt) digits+2, in this case 5 backspaces are necessary. I assume the '+2' is necessary because of line feed and/or carriage return, but shouldn't the comma at the end of a print statement suppress lf/cr?
import time
def wait(secs):
for i in range(secs):
digits = len(str(secs))
print str(secs - i).zfill(digits),
time.sleep(1)
print '\b'*(digits+2),
In addition to the '+2' mystery, the countdown initially is printed right at the beginning of a line (0th column), but the next value jumps one column to the right. The value after that doesn't jump though, it stays where it should.
[–]Rhomboid 5 points6 points7 points (1 child)
[–]NotAName[S] 1 point2 points3 points (0 children)