3

I want to take a timed input. If the user doesn't give the input under a limited amount of seconds, the program moves on. So, I learned about inputimeout() but even when I am giving the input within the time limit, it just waits for the timeout. (Also I am not able to solve the problem from other similar questions and that is why I decided to mention this problem)

from inputimeout import inputimeout, TimeoutOccurred
try:
    something = inputimeout(prompt = 'Enter: ', timeout=5)
except TimeoutOccurred:
    print('Time Over')

Output for the above code:

Enter: e

Time Over

Process finished with exit code 0

Even if I give the input within the time limit, it shows Time Over. Can anyone please help me out?

Chitin
  • 35
  • 6
  • 1
    Does this answer your question? [Python : Skip the input function with timeout](https://stackoverflow.com/questions/66061767/python-skip-the-input-function-with-timeout) – Irfan wani Jun 01 '21 at 05:05

1 Answers1

2

Keeping it simple, it is a module that reads input from the user, but with a twist, it has a timeout placed by the developer, if the program doesn't detect the information from the user, it skips the input. A simple way to use it would be:

timer = 2
var = inputtimeout(prompt='Enter: ', timeout=timer)

That would give the user 2 seconds to type, you can also increment with a trycatch block to give a message to the user in case of a timeout.

lvieira21
  • 46
  • 3
  • Thank you but can you please explain to me why is it skipping the input even after I gave the information? – Chitin Jun 01 '21 at 04:36
  • Hum... never happened to me, mind sharing the code bit that is having this problem? – lvieira21 Jun 01 '21 at 04:39
  • I am getting this problem in the code shared in my question. Then I tried the code you gave too, but even after I gave the input within 2 seconds it shows me timeout error – Chitin Jun 01 '21 at 04:42
  • Are you pressing Enter after typing "e"? – enzo Jun 01 '21 at 04:45
  • yes. But then it waits for the given no. of seconds to be over, then shows Time over – Chitin Jun 01 '21 at 04:49
  • I just copy and paste the exact same code you posted in your question and it's not showing anything when I type "e" and press Enter before 5 seconds. Try reinstalling `inputtimeout`. – enzo Jun 01 '21 at 04:57
  • I tried reinstalling it now. But still the same. Can you please tell me which IDE do you use? – Chitin Jun 01 '21 at 05:34
  • Ok I tried it in VSC now. IT WORKS!! but for some reason it doesn't work in PyCharm and IDLE – Chitin Jun 01 '21 at 05:40