Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone please explain how does inputimeout() in python work? [duplicate]

Tags:

python

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?

like image 280
Chitin Avatar asked Feb 26 '26 21:02

Chitin


1 Answers

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.

like image 187
lvieira21 Avatar answered Feb 28 '26 10:02

lvieira21