Search code examples
pythonwindowsraw-input

Avoiding raw_input to take keys pressed while in a loop for windows


I am trying to make a program which has a raw_input in a loop, if anyone presses a key while the long loop is running the next raw_input takes that as input, how do I avoid that?

I don't know what else to add to this simple question. Do let me know if more is required.

EDIT

Some code

for i in range(1000):
  var = raw_input("Enter the number")
  #.... do some long magic and stuff here which takes afew seconds
  print 'Output is'+str(output)

So if someone presses something inside the magic phase, that is take as the input for the next loop. That is where the problem begins. (And yes the loop has to run for 1000 times).


Solution

  • This works for me with Windows 7 64bit, python 2.7.

    import msvcrt
    
    def flush_input():
      while msvcrt.kbhit():
        msvcrt.getch()