Python:等待用户输入,如果10分钟后仍未输入,则继续执行程序
发布于 2021-01-29 15:05:26
我试过了:
from time import sleep
while sleep(3):
input("Press enter to continue.")
但这似乎不起作用。我希望程序等待用户输入,但是如果10分钟后没有用户输入,请继续执行该程序。
这是python 3。
关注者
0
被浏览
75
1 个回答
-
为什么代码不起作用?
time.sleep
什么也不返回;time.sleep(..)
成为的价值None
;while
循环体未执行。如何解决
如果您使用的是Unix,则可以使用
select.select
。import select import sys print('Press enter to continue.', end='', flush=True) r, w, x = select.select([sys.stdin], [], [], 600)
否则,您应该使用线程。
Windows 使用的
特定解决方案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)