I tried:
from time import sleep
while sleep(3):
input("Press enter to continue.")
But it doesn't seem to work. I want the program to await user input, but if there's no user input after 10 minutes, continue with the program anyway.
This is with python 3.
Why does the code not work?
time.sleep
returns nothing; the value of the time.sleep(..)
become None
; while
loop body is not executed.
How to solve it
If you're on Unix, you can use select.select
.
import select
import sys
print('Press enter to continue.', end='', flush=True)
r, w, x = select.select([sys.stdin], [], [], 600)
Otherwise, you should use thread.
Windows specific solution that use msvcrt
:
import msvcrt
import time
t0 = time.time()
while time.time() - t0 < 600:
if msvcrt.kbhit():
if msvcrt.getch() == '\r': # not '\n'
break
time.sleep(0.1)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments