[Tutor] Still Can't Find Timed While Loops

Ricardo Aráoz ricaraoz at gmail.com
Fri Dec 7 13:18:43 CET 2007


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?
>  
> Here's what I've got.  I'm sure it ain't pretty and I'm happy to hear
> suggestions on cleaning it up as well.  I know it needs some def's in
> there and possibly a class too.
>  
> import random

import time

> startNum = random.choice(range(1, 9))
> print 'Start with the number ', startNum,'.  Then continuously add 7 to
> that number until the timer runs out.  You have 30 seconds.'
> score = 0

  start_time = time.clock()

> answer = int(raw_input('Enter your answer: '))
> while (score < 50):
>     startNum = startNum + 7
>     if startNum == answer:
>         print 'That is correct!'
>         score = score + 5
>         print 'Your score is ', score

      if (time.clock() - start_time) >= 30
          print 'Time's out. Game over. Your final score is ', score
          break

>        
>         answer = int(raw_input('Enter your answer: '))
>     else:
>         print 'That is incorrect.  Game over. Your final score is ', score
>         break
> else:
>     print 'You win! Your final score is ', score
>  
>  



More information about the Tutor mailing list