0

I need help with my code right here. This will obviously be a very simplified version of my code and it only includes the parts that I have a problem with.

import keyboard

print("something something...")
keyboard.wait("enter")

while True:
    player1 = input("Enter name -- ")
    if not player1.isalpha():
        print("Invalid reply!")
        continue
    break

Still, here is the link to my full code if that could be useful to you in helping me with my problem: https://pastebin.com/mDsAdawr

Whenever my code reaches the part keyboard.wait("enter") the input function right after it runs in the background and stores all of the keypresses that I make. For example, if the user types in Hello123 and then presses enter, the input function will assign "Hello123" to the variable player1 thus displaying the error message, even though the user didn't even mean to input that for the variable.

YeetDaddy
  • 28
  • 5
  • It's not the `input()` function doing that, it's the operating system. Anything you type is put into the terminal's input buffer. When you call `input()`, it reads from the buffer. – Barmar Jul 08 '22 at 19:49
  • @Barmar Is there any way to remove/clean the buffer before the input function is called? Also it would be really helpful of you could say this comment as an answer as this is my first time asking a question and I really want to mark someone else as correct answer. – YeetDaddy Jul 08 '22 at 20:12
  • Are you on Linux or Windows? For Linux see https://docs.python.org/3.8/library/termios.html#termios.tcflush – Barmar Jul 08 '22 at 20:14
  • @Barmar I am on Windows. – YeetDaddy Jul 08 '22 at 20:16

0 Answers0