Skip to content
Advertisement

Skip the input function with timeout

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

  1. Import the module in file
  2. start the try method
  3. make a variable and instead of input use inputimeout function and enter values as prompt= and timeout=
  4. In except TimeoutOccurred: enter the value of the var if timeout is occured
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement