2

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?

kaya3
  • 47,440
  • 4
  • 68
  • 97
JIN
  • 149
  • 2
  • 10
  • 1
    Does this answer your question? [Keyboard input with timeout?](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout) – costaparas Feb 05 '21 at 11:04

2 Answers2

2

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
Rajat Soni
  • 151
  • 12
1
  • Note that timeout of only 0.5 seconds won't give the user enough time to type anything. I would suggest giving more time.

You can use the inputtimeout module (available here):

from inputimeout import inputimeout, TimeoutOccurred

def functionA():
    pass


def functionB():
    pass

#TIMEOUT = 0.5
while True:
    TXT = None
    try:
        TXT = inputimeout(prompt = "Enter: ", timeout=0.5)
    except TimeoutOccurred:
        TXT = None

    if TXT == None:
        functionA()
    elif 'NAME' in TXT:
        functionB()
    elif TXT == 'EXIT':
        break
    else:
        pass
Jakub Szlaur
  • 1,852
  • 10
  • 39