[Tutor] While Loops and Modules
earlylight publishing
earlylightpublishing at yahoo.com
Thu Dec 6 06:44:09 CET 2007
Hello again to all the wonderfully helpful folks on this list. Today I did my Google homework and I found this neat bit of code for a countdown timer.
import time
import threading
class Timer(threading.Thread):
def __init__(self, seconds):
self.runTime = seconds
threading.Thread.__init__(self)
def run(self):
time.sleep(self.runTime)
print "Buzzz!!! Time's up!"
t = Timer(30)
t.start()
I don't understand large chunks of it (don't know what threading, self, or __init__ mean) but that's not important at the moment. It works and I will learn the vocab eventually.
I also wrote this bit of code for a math challenge which comes in the next part of my game.
import random
startNum = random.choice(range(1, 9))
newNum = startNum + 7
score = 0
print 'Start with the number ', startNum, '. Then continuously add 7 to that number until the timer runs out. You have 30 seconds.'
answer = int(raw_input('Enter your answer: '))
if newNum == answer:
print 'That is correct! Keep going.'
score = score + 5
print 'Your score is ', score
else:
print 'That is incorrect. Please try again.'
I understand this part just fine 'cause I actually wrote it myself.
What I need to do now is put these two parts together so that the player will keep adding 7 to the starting number for 30 seconds then the loop breaks. I know I need a loop of some sort and I'm guessing it's a 'while' sort of thing. I couldn't find what I was looking for when I Googled. I'm not even sure I knew the right search terms. Does anyone know how I'd combine these two modules to make it work?
---------------------------------
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/20071205/e265b7d9/attachment.htm
More information about the Tutor
mailing list