Reputation: 91
My current code is the following:
for a in xrange (60, 0, -1):
b = "Sleeping for " + str(a) + " seconds\r"
print b,
sleep(1)
The only issue I have with it is that when it goes from 2 digit numbers to 1 digit numbers it prints secondss instead of seconds. How can I modify my code to properly replace the line when a goes from 2 digits to 1. Also, after the script is done printing the "Sleeping for __ seconds" lines am I able to replace that with a line that says "Sleeping for 60 seconds complete"
Upvotes: 0
Views: 753
Reputation: 16184
How about using string formatting?
for a in xrange (60, 0, -1):
print "Sleeping for %2d seconds...\r" % a,
sleep(1)
print "Sleeping for 60 seconds complete!"
Upvotes: 0
Reputation: 555
you can print out an equal-length string of spaces to cover it up:
for a in xrange(60, 0, -1):
out = "sleeping for {} seconds\r".format(a)
sys.stdout.write(out)
sys.stdout.flush()
time.sleep(1)
clear = " " * len(out)
sys.stdout.write(clear + "\r")
print "Done"
you can also add ellipses to out
to cover it up:
for a in xrange(60, 0, -1):
out = "sleeping for {} seconds..\r".format(a)
sys.stdout.write(out)
sys.stdout.flush()
time.sleep(1)
print "Done"
Upvotes: 0
Reputation: 11
You need to remove the comma in the print line to see the print output
Upvotes: 1