How do I disable user input for turn based play?

I am trying to create a basic math game on the command line in Python 3. The game shows the user a basic add problem, the system waits for the user to hit enter, then displays the answer and then the next problem.

I want to put pressure on the player by including a timeout per turn. If the player hasn't pressed the enter button within 3 seconds, the game is stopped and a message is displayed.

Problem: How to interrupt input()

after 3 seconds.

Research so far:

  • Python multithreading interrupt input ()
  • https://docs.python.org/3/library/signal.html

Illustrative code snippet:

import signal

import sys


def ask_question():
    input("3 + 4 = ?")


def print_answer():
    print("7")


def timeout_handler(signum, frame):
    print("sorry you ran out of time")
    sys.exit()

MAX_TURN_TIME = 3

ask_question()
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(MAX_TURN_TIME)
print_answer()
signal.alarm(0)

      

+3
python python-3.x


source to share


No one has answered this question yet

See similar questions:

248
Function call timeout
49
Timeout keyboard input?
1
Python multithreading interrupt input ()

or similar:

515
Prompt the user for input until they give a valid answer
432
User input and command line arguments
3
Supersede users need to hit enter after typing a line in python 3.0
2
user input using python
1
Python multithreading interrupt input ()
1
Python3 asks for user input for a limited time, then enter the default if the user is not responding
1
Delaying user input in Python
1
How to not output a newline in Python 3 when typing text using input ()
0
giving turns for attacks in a mini-game (python 2.7)
0
How to implement multiple signals in a program?



All Articles
Loading...
X
Show
Funny
Dev
Pics