0

How to continue the program without waiting for a user input? I want the loop to continue after 2 seconds even without user input, and break if the user type quit. This code is not working, because I need to manually hit enter every time to be able to continue the loop.

while True:
    answer = input("please enter quit to stop the program")
    time.sleep(2)  # wait for 2 seconds for the possible user input "quit"
    if answer == "quit":
        break
    ... other code

operation system: windows 7, python 3.6

I have read: Python: wait for user input, and if no input after 10 minutes, continue with program and: raw_input and timeout. I tried the answers for windows and none of them worked. I didn't completely understand them either. Could you please comment on the major steps in the code? Thank you so much!

Yang
  • 177
  • 4
  • 20
  • I don't get why you use `time.sleep` here. `input` waits until the user hits ENTER. – Willem Van Onsem Feb 01 '18 at 11:58
  • 1
    I believe this answers it?https://stackoverflow.com/questions/19508353/python-wait-for-user-input-and-if-no-input-after-10-minutes-continue-with-pro – pm1391 Feb 01 '18 at 11:59
  • Because I tried to wait 2 seconds for the user to input, even if he doesn't input anything, after 2 seconds, the program continue. I tried all the methods in the other two threads, none of them worked.. – Yang Feb 02 '18 at 11:06
  • `r, w, x = select.select([sys.stdin], [], [], 600)` # where 600 is timeout in seconds, check: select.select for details – Andrew_1510 Mar 21 '19 at 06:38

0 Answers0