1

I want the python program to wait for user specific char input, let's say "r"/"f", and to continue according to this char.

If after n hours none of these were input, I want the program to do something else. How can this be reached?

2
  • This contains the answer you're looking for: docs.python.org/3 Commented Apr 28, 2022 at 13:28
  • Does this answer your question? Time-Limited Input? Commented Apr 28, 2022 at 13:28

1 Answer 1

2

Just use this

from threading import Timer

timeout = 10 #here is the time in second
t = Timer(timeout, print, ['Sorry, times up'])
t.start()
prompt = "You have %d seconds to choose the correct answer...\n" % timeout
answer = input(prompt)
t.cancel()
Sign up to request clarification or add additional context in comments.

1 Comment

When I'm doing this, script is still waiting for input even though I get "Sorry, times up" when no input was inserted. I want it to skip the waiting for input with timeout has passed.

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.