How To Replace String From Previous Line Using \r (python)
I'm attempting to make a countdown timer starting from 60 seconds. My only problem is that when I run the function, the program prints the code like so: 60 59 58 ... etc How would
Solution 1:
Print an empty line at the beginning of your countdown
function. This will create the line needed for \r
to work. Now, replace the print seconds
part with
sys.stdout.write('\r \r')
sys.stdout.write(str(seconds))
The first line clears the previous, empty line printed at the beginning of the function. The second one outputs the seconds.
The code:
def countdown():
seconds = 60print''while seconds >= 0:
sys.stdout.write('\r \r')
sys.stdout.write(str(seconds))
sys.stdout.flush()
time.sleep(1)
seconds -= 1
Post a Comment for "How To Replace String From Previous Line Using \r (python)"