0

I have the following code. I want to add if the user does not give any input nor he/she presses the Enter key, the code should move ahead.

    while True:
        user_input = input("enter here=")

        if user_input == int(user_input):
            result = 5 + user_input
            print(result)
# If the user does not give any input or presses the Enter key in 5 seconds
        else:
            continue
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

3 Answers3

0

The part of code if user_input==int(user_input) is never true because input is a string by default.

lemon
  • 14,875
  • 6
  • 18
  • 38
0

You can do this by utilising the SIGALRM signal. Here's a pattern that you could implement:

from signal import signal, SIGALRM, alarm

TV = 5

class Timeout(Exception):
    ...

def get_input():
    def handler(*_):
        raise Timeout
    old_handler = signal(SIGALRM, handler)
    alarm(TV)
    try:
        return input(f'Enter something. You have {TV} seconds to respond: ')
    except Timeout:
        print('Timed out')
    finally:
        signal(SIGALRM, old_handler)

print(get_input())
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

Jllobvemy's answer to this question might be what you're looking for if you're not looking to understand exactly how it works. It uses a simple, easy-to-understand module.

There are more complicated solutions as well, but those might help you understand the complexity of setting a timeout on an input in Python. For example, using threads to count how much time has passed can be tricky and easily done wrong.

I also found another simple module named pytimedinput that could be of use in your case. I didn't test it myself, but I thought I'd put it here anyway so you can choose what suits you best. Beware though, as pytimedinput only works in a console, not in IDEs apparently.

An example for pytimedinput:

from pytimedinput import timedInput

userText, timedOut = timedInput("Please, do enter something: ", resetOnInput=False)
    #resetOnInput defaults to True, which resets the timer on each keypress. This is probably not what you're looking for.


if(timedOut):
    print("Timed out when waiting for input.")
    print(f"User-input so far: '{userText}'")

else: print(f"User-input: '{userText}'")

input()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
diogeek
  • 141
  • 9
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 05 '23 at 13:43
  • Hi, I am getting follwing error. RuntimeError: timedInput() requires an interactive shell, cannot continue. Running following code........from pytimedinput import timedInput userText, timedOut = timedInput("Please, do enter something: ") if(timedOut): print("Timed out when waiting for input.") print(f"User-input so far: '{userText}'") else: print(f"User-input: '{userText}'") – Rakesh Mulik Jun 05 '23 at 15:12
  • Also, the second answer does not let me input anything – Rakesh Mulik Jun 05 '23 at 15:17
  • @RakeshMulik You get this error because pytimedinput apparently only works in console, not in your IDE. Also, it seems there is by default a parameter called `ResetOnInput` that is by default set to `True`. This parameter resets the timer on each input. This is probably not what you want. I'll update my answer. – diogeek Jun 05 '23 at 15:27
  • Also, I think (feel free to correct me) that you thought you couldn't input anything with the second answer (the one using threads?). You can. the prompt is just a little unclear. Try inputting something and retry without inputting anything for ten seconds. – diogeek Jun 05 '23 at 15:31