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



我想让python程序等待用户特定的字符输入,比如" "/"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()

最新更新