3

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?

1

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this does not work in IDLE (and according to another user also not in PyCharm, but I can't confirm that). Does anyone have a solution that does work in IDLE?
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.