I am using time.sleep(10) in my program. Can display the countdown in the shell when I run my program?
>>>run_my_program()
tasks done, now sleeping for 10 seconds
and then I want it to do 10,9,8,7....
is this possible?
I am using time.sleep(10) in my program. Can display the countdown in the shell when I run my program?
>>>run_my_program()
tasks done, now sleeping for 10 seconds
and then I want it to do 10,9,8,7....
is this possible?
This is the best way to display a timer in the console for Python 3.x:
This writes over the previous line on each cycle.
Sure, just write a loop that prints 10 minus the iteration counter, then have it sleep 1 second each iteration and run for 10 iterations. Or, to be even more flexible:
time.sleep()
may return earlier if the sleep is interrupted by a signal or later (depends on the scheduling of other processes/threads by OS/the interpreter).To improve accuracy over multiple iterations, to avoid drift for large number of iterations, the countdown may be locked with the clock:
This is something that I've learned at one of my first python lessons, we played with ["/","-","|","\","|"] but the principle is the same:
you could always do
This snippet has the slightly annoying feature that each number gets printed out on a newline. To avoid this, you can
Here's one I did:
At the end if you and an else you can put an alarm or whatever.