0

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.

Community
  • 1
  • 1
Miki
  • 65
  • 1
  • 2
  • 7
  • 2
    http://stackoverflow.com/questions/3471461/raw-input-and-timeout – Alexey Kachayev Dec 16 '12 at 12:43
  • Why are you asking this again? http://stackoverflow.com/questions/13893287/python-time-limit – Jon Clements Dec 16 '12 at 12:50
  • 2
    Improve the original question if you want it to get more attention. Duplicate questions are always closed. – Lev Levitsky Dec 16 '12 at 12:54
  • 1
    What @LevLevitsky says, plus improve it with what efforts/problems you've had in the 21 hours since you got an answer that you appear to have ignored. – Jon Clements Dec 16 '12 at 12:58
  • I improved my question. Can you help me now? – Miki Dec 16 '12 at 13:04
  • 2
    @Miki Now it is not a duplicate. Unfortunately, it got closed anyway. What I suggest is that you go back to the original question (from 22 hours ago) and update it with new information: write something like "I tried code from the answer by sys.stderr, but it doesn't quite work because...". This will bump the old question and get you more views and will show your intention to improve your posts. – Lev Levitsky Dec 16 '12 at 13:14

0 Answers0