[Tutor] stop a loop after precise amount of time

Peter Otten __peter__ at web.de
Sun Nov 25 12:45:37 CET 2012


Saad Javed wrote:

> import time
> 
> running = True
> while running:
>     print 'yes'
>     time.sleep(10)
> 
> This will print 'yes' after every 10s. I want to print 'yes' for 10s, then
> quit.

Then combine the two techniques, the busy waiting loop with sleeping for a 
shorter amount of time:

import time

stop_time = time.time() + 10
while time.time() < stop_time:
    print "yes"
    time.sleep(.1)
print "that's all folks"




More information about the Tutor mailing list