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

2024-04-20 15:27:09 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在不等待用户输入的情况下继续程序? 我希望循环在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

操作系统:Windows7、Python3.6

我读过:Python: wait for user input, and if no input after 10 minutes, continue with program 和:raw_input and timeout。 我试过窗户的答案,但没有一个奏效。我也不完全理解他们。 请您对代码中的主要步骤发表意见好吗?非常感谢你!在


Tags: andthe代码用户answer程序forinput