[Tutor] Still Can't Find Timed While Loops

bob gailer bgailer at alum.rpi.edu
Fri Dec 7 19:20:44 CET 2007

bhaaluu wrote:
> On Dec 7, 2007 10:41 AM, bob gailer <bgailer at alum.rpi.edu> wrote:
>> earlylight publishing wrote:
>>> Hello all,
>>> I now have my bit of code in while loop form and it works!  It's great
>>> but not exactly what I wanted to do.  I've been googling my heart out
>>> and I find lots of info on while loops and lots of info on timers that
>>> will execute an action AFTER a given interval but nothing on a timer
>>> that will execute an action DURING a given interval.  What I'd really
>>> like to do at this point in my game is have the player execute the
>>> loop for 30 seconds then have it print the final score and break.
>>> Does anyone out there have any code that'll do that?
>> import time
>> start = time.time()
>> raw_input("Wait a few seconds, then press enter to continue.")
>> print time.time() - start
>> Does that give you a hint?
>> You don't need a timer, unless you want to interrupt the user if he is
>> not entering anything.
> Cool snippet! So if the player is entering numbers to an adding game,
> and he has to continue entering numbers for 30 seconds, then it could
> be accomplished with something similar to this, right?
> import time
> start = time.time()
> while 1:
>     raw_input("Press <Enter> Now!")
>     if time.time() - start < 30.0:
>         print time.time() - start
>     else: break
Even simpler?

import time
start = time.time()
while time.time() - start < 30.0:
    raw_input("Press <Enter> Now!")
    print time.time() - start

More information about the Tutor mailing list