首页 > 解决方案 > 如何在python超时后跳过用户输入

问题描述

我希望能够接收这样的用户输入:

while True:
    curr_time = time.time()
    timeout = curr_time+30 // after 30 seconds the bottom input code should be skipped
    message = input("Your message: ")
    //the rest of the code

大约 30 秒后,代码应该继续执行其余的代码,然后在 while 循环中重新开始,在该循环中将再等待 30 秒,然后才会跳过输入选项并继续循环......

标签: python-3.xuser-input

解决方案


这是一个可能有帮助的代码片段?

# get the character of a key pressed (no return key needed)
# works only in the command window and with Windows OS
from msvcrt import getch
print "press a char key (escape key to exit)"
while True:
    z = …

Well you could just use a Thread object to do the counting for you and then in your main program everytime the user enters a key, you just call a function of the Thread object counting that returns it's current value.

The example below might help.

# counterTest.py, …

推荐阅读