Search code examples
pythoninputconsole

Python read whatever is entered into console every second


I'm writing a program that needs to read console input every second, you're intended to type a number 1-3 and every second the program will check if one of those numbers is currently entered, then save that input for later use, clear the console, and print some updated values

for example, if I type 1 into the console and dont press enter, the program will read that at the end of the second and save that 1 as a string.

I already have a working timer that clears the console and prints new information every second, but at the moment, if something is entered, then at the end of the second its just cleared away


Solution

  • If I understand correctly the problem is that you can't read inputs without the user pressing enter.

    If you are on windows I would use msvcrt It should be preinstalled so the code would look a bit like

    import msvcrt
    
    while gameloop: #just the gameloop
        # your code and all
        input_char = msvcrt.getch(); #as a not this does return a char rather then a string
        if (input_char.lower() == "some letter or number"):#do stuff...