0

I want to make it so that after a certain time (depending on the difficulty that the user has chosen) the program continues with the code and the user can no longer enter anything.

This is what I have so far:

t = Timer(
     getTime(),
     print,
     ("Calculating...",)
)
t.start()
answer = input()
t.cancel()

This is the getTime function:

def getTime():
    if difficulty == "easy":
        time = 1
    elif difficulty == "intermediate":
        time = 0.75
    elif difficulty == "hard":
        time = 0.5
    return time

What happens currently is that the thread ends and prints out "Calculating..." but the user is still allowed to input.

Thank you.

Zurteh
  • 1
  • if you want to use thread [this](https://stackoverflow.com/questions/34562473/most-pythonic-way-to-kill-a-thread-after-some-period-of-time) may be helpful – NicoCaldo May 04 '22 at 14:04
  • @NicoCaldo, that thread doesn't seem useful at all, since it is about killing Python threads. This question seems to be about aborting the blocking call to `input()` (which I don't think is possible). I think this problem can only be solved using a different (non-blocking) input method. – wovano May 04 '22 at 15:20
  • @wovano you put the input inside a thread and after a certain period of time you kill the thread, and with it the input. It's actually well described [here](https://stackoverflow.com/questions/2408560/non-blocking-console-input) by Marco – NicoCaldo May 04 '22 at 15:25
  • Does this answer your question? [Non-blocking console input?](https://stackoverflow.com/questions/2408560/non-blocking-console-input) – wovano May 04 '22 at 15:26
  • @NicoCaldo, that's a completely different solution, and uses what I was referring to as "a different input method". But thanks for providing the second link! I think this is exactly what OP is looking for. – wovano May 04 '22 at 15:27
  • 1
    Just for reference, putting `input()` inside the thread won't work. You need to use something like `msvcrt` on Windows or `termios` in Linux. – wovano May 04 '22 at 15:29
  • Ah, okay, I just saw the answer from Marco, which _does_ indeed use `input()`, and that _could_ work for this scenario, although you can't really (gracefully) kill the keyboard thread. Anyway, the link provides several different solutions with different pros and cons. – wovano May 04 '22 at 15:33
  • @wovano thank you so much, ill give these a shot and ill let you know if they worked. – Zurteh May 04 '22 at 19:31

0 Answers0