1

I am writing my own game about mental operations in your head such as addition, subtraction, multiplication and division. Right now, the game is running smoothly with addition and subtraction operations but what I want now is to ask to the user his/her guess but if he/she delays 5 seconds the input message should disappear, the correct result should appear and another operation should appear. This is the following code for the game:

def level_one():
    goodGuess=0
    badGuess=0
    time_easy_level = default_timer()
    numbers_with_operators=[]
    local_operators_easy=["+","-"]
    global continuePlaying
        #====Repeat operations 5 times=================
    for x in range(5):
        #===10 random numbers between 1 and 10 are generated =========
        easy_level=[randint(1,10) for i in range(1,10)]
            #===Each list of random numbers is appended with a random operator===
        for item in easy_level:
            numbers_with_operators.append(item)
            time.sleep(1)
            numbers_with_operators.append(local_operators_easy[randint(0,1)])
            if len(numbers_with_operators)==18:
                numbers_with_operators.append(randint(1,10))
            print numbers_with_operators
        time_for_guess=time.time()
        deadline_for_guess=time_for_guess+5
        while time_for_guess<deadline_for_guess:
            user_guess=int(raw_input("What is the result? "))
            break
        computer_result=compute_list(numbers_with_operators)
        if user_guess==computer_result:
            goodGuess+=1
            print "Good guess!"
        else:
            print "Sorry, that is not the result"
            badGuess+=1
            print computer_result
        del numbers_with_operators[:]
    duration=default_timer()-time_easy_level
    continuePlaying=False
    print "Your results are: \n"
    print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
    print "Total seconds playing:\n"+str(duration)+" seconds"
    return continuePlaying

All suggestions are welcome and feel free to modify my code :)

abautista
  • 1,759
  • 4
  • 26
  • 48

2 Answers2

0

See this post.

Your current while loop does not work; that is not really proper usage of it and it just waits endlessly for you to give an input. In your case, I think a good approach would be to define a function to ask the user for answers that is called recursively each time threading.Timer expires. Some sample code:

import threading
def guess_a_number():
    #generate random numbers
    #generate operators
    timer = threading.Timer(5.0, guess_a_number())
    timer.start()
    #get user input
    #check if user input is correct
    if True:
        print "You were right!"
        guess_a_number()
    else:
        print "Sorry, you were wrong."
        guess_a_number()

I believe this should make the function start over on expiration of the timer, or on a response (whether correct or incorrect). The specifics are obviously up to you.

Edited for typo.

Community
  • 1
  • 1
aenda
  • 141
  • 4
0

Thanks to the suggestions made by @aenda and @lennon310 and doing some research on the following posts: Non-blocking raw_input code from Gary Robinson, Raw input and timeout and Keyboard input with timeout in python I was able to do some slight changes and generate the following code that solves my problem.

timeout=7

class AlarmException(Exception):
    pass

def alarmHandler(signum,frame):
    raise AlarmException

def my_raw_input(prompt,timeout):
    signal.signal(signal.SIGALRM,alarmHandler)
    signal.alarm(timeout)
    try:
        userGuess=int(raw_input(prompt))
        signal.alarm(0)
        return userGuess
    except AlarmException:
        print "Uh-oh! Time's for that one!"
    signal.signal(signal.SIGALRM,signal.SIG_IGN)
    return ''

def level_one():
    goodGuess=0
    badGuess=0
    time_easy_level = default_timer()
    numbers_with_operators=[]
    local_operators_easy=["+","-"]
    global continuePlaying
    global timeout
    #====Repeat operations 5 times=================
        for x in range(5):
        #===10 random numbers between 1 and 10 are generated =========
            easy_level=[randint(1,10) for i in range(1,10)]
            #===Each list of random numbers is appended with a random operator===
            for item in easy_level:
                numbers_with_operators.append(item)
                time.sleep(1)
                numbers_with_operators.append(local_operators_easy[randint(0,1)])
                    if len(numbers_with_operators)==18:
                        numbers_with_operators.append(randint(1,10))
                print numbers_with_operators
                user_guess = my_raw_input("What's the result? ",timeout)
                computer_result=compute_list(numbers_with_operators)
                if user_guess==computer_result:
                    goodGuess+=1
                    timeout=7
                    print "Good guess!"
                else:
                    print "Sorry, that is not the result"
                    badGuess+=1
                    timeout=7
                    print computer_result
              print "That was the number operation "+str(x)
              del numbers_with_operators[:]
        duration=default_timer()-time_easy_level
        continuePlaying=False
        print "Your results are: \n"
        print "Good guesses: "+str(goodGuess)+"\nBad guesses: "+str(badGuess)
        print "Total seconds playing:\n"+str(duration)+" seconds"
        return continuePlaying
Community
  • 1
  • 1
abautista
  • 1,759
  • 4
  • 26
  • 48