我希望Python程序等待用户特定的字符输入,比如说“r”/“f”,并根据这个字符继续。
如果n小时后没有输入这些内容,我希望程序执行其他操作。 如何才能达到这个目标?
就用这个
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()
同样的行为。如果超时,程序永远不会退出