I’m making any program in Python 3.7.
I want to skip input
function after a specific time.
My code has the structure like the following rough code.
def functionA(): ...(skip)... def functionB(): ...(skip)... #TIMEOUT = 0.5 while True: TXT = None TXT = input("Enter: ") if TXT == None: functionA() elif 'NAME' in TXT: functionB() elif TXT == 'EXIT': break else: pass
I wanna skip the line TXT = input("Enter: ")
after TIMEOUT
time, 0.5 sec. How can I make the code of this flow the way I want?
Advertisement
Answer
You can use the inputimeout
module
You can install the module by running cmd and typing this command
pip install inputimeout
You can use it like this
from inputimeout import inputimeout, TimeoutOccurred try: var = inputimeout(prompt='>>', timeout=5) except TimeoutOccurred: var = ''
Steps to use
- Import the module in file
- start the try method
- make a variable and instead of input use
inputimeout
function and enter values as prompt=
andtimeout=
- In
except TimeoutOccurred:
enter the value of the var if timeout is occured