1

I am using the input() function in python 3, and I noticed that keystrokes made while the program is running will be dumped onto the screen all at once at the next input() call. This is bearable but mildly annoying, except for when the enter key is pressed during the program, which I believe is making the input automatically skip because it sees the newline character and takes that as the end of input.

for example, in a program like this:

time.sleep(5)
input("Enter something: ")

typing enter during the sleep() would immediately skip the following input.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • That sounds a bit annoying :/ This might be related: https://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream – Michael Apr 14 '22 at 23:35
  • That's because there's a keyboard buffer so they're stored until read. – martineau Apr 14 '22 at 23:37
  • this is because your keystrokes are stored in a buffer and python's input function reads data from that buffer so it doesn't matter when you press the key, it reads it. you can flush the buffer before getting new input. – Mohammad Salehi Apr 14 '22 at 23:38
  • The operating system automatically buffers keyboard input. It takes extra code to flush this buffer before reading input, and Python doesn't do it automatically. – Barmar Apr 14 '22 at 23:38
  • Thank you everyone for the help. I was not aware of the keyboard buffer, this will be very helpful – Carter Allen Apr 14 '22 at 23:43

0 Answers0