Hey there, I would like to give a timeout on my Python inputs. If possible, I would like the code to be portable so that I can give timeouts to everything else I do.
Please do not answer with the timeout decorator I rather not that since I tried it already
Thanks for those who help :)
This was my attempt to create a timed input function:
import time def timed_input(prompt,t): start = time.time() inp = input(prompt) now = time.time() if now - start < t: return inp else: return "Timed Out"
Edit: It works!
I thought the code for input would wait until it receive an input, but I don't think that's the case. The answer is more complicated and lies in threading which is used to run multiple threads (functions, tasks, etc) at the same time. This way it can measure the time at the same time it is asking for input. I don't know much about threading in Python but Stack Overflow does :)
@LoneAce There's no way to retract the input without using threading after a given amount of time but the function I wrote above returns 'Timed Out' if they took longer than the specified time. You can use that to tell the user that they took too much time. Preventing input is not necessarily the solution and is probably more complicated than you need.