Python:当用户输入退出时循环停止,即使没有输入也会继续

时间:2018-02-01 11:49:40

标签: python python-3.x

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

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的答案,但没有一个工作。我也不完全理解他们。 您能否对代码中的主要步骤发表评论?非常感谢你!

0 个答案:

没有答案
相关问题