这个问题在这里已有答案:

如何在不等待用户输入的情况下继续程序?即使没有用户输入,我希望循环在2秒后继续,如果用户输入则退出 . 这段代码不起作用,因为我需要每次手动点击输入才能继续循环 .

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

操作系统:windows 7,python 3.6

我看过:Python: wait for user input, and if no input after 10 minutes, continue with program和:raw_input and timeout . 我尝试了Windows的答案,但没有一个工作 . 我也没完全理解他们 . 您能否对代码中的主要步骤发表评论?非常感谢!