Countdown display for python wait function

I am using time.sleep (10) in my program. Can a shell countdown be displayed when my program starts?

>>>run_my_program() tasks done, now sleeping for 10 seconds 

and then I want him to do 10,9,8,7 ....

Is it possible?

+9
source share
7 answers

you could always do

 #do some stuff print 'tasks done, now sleeping for 10 seconds' for i in xrange(10,0,-1): time.sleep(1) print i 

This fragment has a slightly annoying feature - each number is printed on a new line. To avoid this, you can

 import sys import time for i in xrange(10,0,-1): sys.stdout.write(str(i)+' ') sys.stdout.flush() time.sleep(1) 
+17
source

You can make a countdown function, for example:

 import sys import time def countdown(t, step=1, msg='sleeping'): # in seconds pad_str = ' ' * len('%d' % step) for i in range(t, 0, -step): print '%s for the next %d seconds %s\r' % (msg, i, pad_str), sys.stdout.flush() time.sleep(step) print 'Done %s for %d seconds! %s' % (msg, t, pad_str) 

A carriage return \r and a comma will keep printing in the same line (avoiding one line for each countdown value)

As the number of seconds decreases, pad_str will ensure that the last line is overwritten with spaces, instead of leaving the last character after cutting the output.

Final printing overwrites the last status message with the completed message and increases the output line, so there is evidence of a delay.

+7
source

This is the best way to display a timer in the console for Python 3.x:

 import time import sys for remaining in range(10, 0, -1): sys.stdout.write("\r") sys.stdout.write("{:2d} seconds remaining.".format(remaining)) sys.stdout.flush() time.sleep(1) sys.stdout.write("\rComplete! \n") 

This is written on the previous line in each cycle.

+5
source

Of course, just write a loop that prints 10 minus the iteration counter, and then falls asleep 1 second per iteration and starts 10 iterations. Or, to be even more flexible:

 def printer(v): print v def countdown_timer(duration, step=1, output_function=printer, prompt='Waiting {duration} seconds.'): output_function(prompt.format(duration=duration)) for i in xrange(duration/step): output_function(duration - step * i) 
+1
source

Here is one I did:

 import time a = input("How long is the countdown?") while a != 0: print a time.sleep(1) a = a-1 

In the end, if you and you can still set an alarm or something else.

+1
source

time.sleep() may return earlier if the failure is interrupted by a signal or later (depends on the scheduling of other processes / threads using the OS / interpreter).

To increase accuracy over multiple iterations, to avoid drift for a large number of iterations, the countdown can be blocked using a clock:

 #!/usr/bin/env python import sys import time for i in reversed(range(1, 1001)): time.sleep(1 - time.time() % 1) # sleep until a whole second boundary sys.stderr.write('\r%4d' % i) 
+1
source

This is what I learned in one of my first python lessons, we played with ["/", "-", "|", "\", "|" ], but the principle is the same:

 import time for i in reversed(range(0, 10)): time.sleep(1) print "%s\r" %i, 
+1
source

All Articles