0

I'm looking for a way to have python stop a user input from happening if it's not done in enough time. The code contains a timer which currently does countdown properly but it waits for user input to check any if statements made. I need it to end the while loop if the user input isn't made within 15 seconds which will in-turn display the user losing.

def timer(tim):
    time.sleep(1)

def hall():
    print("Where will you go?")

    tim = 15
    while(tim > 0):

        direction = input()

        if(direction == "left"):
            print("you went left")
        if(direction == "right"):
            print("you went right")
        if(direction == "forward"):
            print("you went forward")
        timer(tim)
        tim-=1
        break
    print ("you took to long, you lose")

Every solution I was pointed to is for a different version of python and uses raw_input which is not in Python 3.

Tippy
  • 1
  • 2
  • 1
    There are lots of solutions to this problem, such as https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python, https://stackoverflow.com/questions/2933399/how-to-set-time-limit-on-input, https://stackoverflow.com/questions/492519/timeout-on-a-function-call, https://stackoverflow.com/questions/15528939/python-3-timed-input – Dartmouth Sep 29 '16 at 06:29
  • You don't need the "break" statement at the end of your while loop. It just ends that loop. – erols Sep 29 '16 at 05:25

0 Answers0