ARTICLE AD BOX
This happens because the input is buffered by your operating system or shell. The keystrokes pressed during time.sleep are still considered input, and then input() sees that input is already there. Most of the time, these terminal applications/games, in your case, would use a library that wraps around the terminal, putting it in a raw terminal mode. Curses are the widely used one for Python:
In your case, you could just use curses.flushinp() which would flush the input line before you take in your input. I would look into the official curses documentation and how to make a curses session:
time.sleep() does not block keyboard input. While your program is sleeping, the terminal continues to buffer keystrokes at the OS level. When input() is finally called, it immediately reads whatever is already in the input buffer — including characters typed during the sleep.
This behavior is expected and cannot be prevented using input() alone.
import time import msvcrt print("Starting") time.sleep(3) while msvcrt.kbhit(): msvcrt.getch() input()Keystrokes are buffered by the terminal, not Python
input() simply reads from that buffer
Clearing the buffer ensures only input typed after the sleep is read
For interactive or real-time terminal programs (such as games), input() is generally unsuitable; libraries like curses or non-blocking input methods should be used instead.
New contributor
Platonic09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Explore related questions
See similar questions with these tags.
