Possible Duplicate:
Python time limit
I have a homework to do and I really need a solution. I have been trying to do this till yesterday but i do not know how.
Program has to generate and print a letter or a number and than a user has to type it as quickly as possible and press ENTER. The game is over after 30 secs.
Well I do not know how to put time limit to a game. I was searching through stackoverflow and I did not find anything useful. Please help me.
Here it is what I have done so far. But I need to improve this code. Because when the 30 secs are over game should be also over but here in this code is over when I type last charachter. I need to break it at some point but I do not know how. After 30 seconds program should still offer to user to write a last letter/number but it can not be counted in results.
max_time =30
start_time = time.time() # remember when we started
while (time.time() - start_time) < max_time:
response = "a" # the variable that will hold the user's response
c = "b" #the variable that will hold the character the user should type
score = 0
number = 0
c = random.choice(string.ascii_lowercase + string.digits)
print(c)
number = number + 1
response = input("Type a letter or a number: ") #get the user's response
if response == c: #if the response from the previous loop matches the character
score = score + 1 #from the previous loop, increase the score.
Thank you very much.