想在python中返回一个键

2024-09-22 16:40:42 发布

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

我想要一个脚本,将返回按键一旦用户按下即使它有时间限制。我使用时间限制让用户输入返回,但我的代码无法工作。我尝试了以下方法。在

def answer(timeout):
    print "Enter the answer"
    stop = time.time() + timeout
    r=['a','b','c','d']
    while True:
        if msvcrt.getche() in r:
            return msvcrt.getche()

        else:
            if time.time() > stop:
                return 'O'

但在超时前我只能得到“ÿ”,即使我按下任何键,也不会被检测到。 这里我需要一个代码,当按下时返回a | b | c | d,当时间超过时返回O。在


Tags: 方法代码用户answer脚本returniftime