Python3 中超时等待用户特定输入

问题描述 投票:0回答:2

我希望Python程序等待用户特定的字符输入,比如说“r”/“f”,并根据这个字符继续。

如果n小时后没有输入这些内容,我希望程序执行其他操作。 如何才能达到这个目标?

python python-3.x user-input sleep
2个回答
2
投票

就用这个

from threading import Timer

timeout = 10 #here is the time in second
t = Timer(timeout, print, ['Sorry, times up'])
t.start()
prompt = "You have %d seconds to choose the correct answer...\n" % timeout
answer = input(prompt)
t.cancel()

0
投票

同样的行为。如果超时,程序永远不会退出

© www.soinside.com 2019 - 2024. All rights reserved.