advice on this little script

Paul Rubin http
Thu Mar 9 06:06:05 CET 2006


John Salerno <johnjsal at NOSPAMgmail.com> writes:
> > for x in range(minutes,0,-1):
> >     sleep(60.0)
> >     print minutes, 'minutes remaining'
> >
> Nice! Cross off another line! I feel like Hemingway.  :)

Besides the bug mentioned, I don't think you should really do it that
way, since sleep(60.0) might not sleep for exactly 60 sec (it could be
longer or shorter).  Preferable is something like (untested):

   now = time.time()
   sleep_until = now + 60*minutes
   while now < sleep_until:
      print int((now - sleep_until)/60), 'minutes remaining'
      sleep (60)
      now = time.time()

You might actually want to stop sleeping a little bit early (say if
you wake up 5 seconds before sleep_until), or round the message to the
nearest number of minutes, etc.



More information about the Python-list mailing list