Skip to content
Advertisement

Clear input buffer before input() on Windows?

I’m creating a game in which you have to guess the result of an operation in a certain time; to do this i created a thread to have a timed input and then I close the thread after the time is up.

Issue

My problem is that when the user doesn’t enter any number, before the time is up, in the next cycle the input waits for two inputs instead of one.

So, what I need is to clear the stdin before getting a new input. I find some solutions for Unix but nothing for Windows.

Code

JavaScript

Advertisement

Answer

There is a pending input() if a timeout happened. You can send an Enter in this case; I’m applying the pyautogui module to do that. (Call is_alive() after join() to decide whether a timeout happened.).

Some runtime errors fixed (and some debugging outputs kept) in the following script:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement