[Tutor] Still Can't Find Timed While Loops
earlylight publishing
earlylightpublishing at yahoo.com
Fri Dec 7 06:11:42 CET 2007
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
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
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
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
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071206/80f190f4/attachment.htm
More information about the Tutor
mailing list