热门Python输入循环

1 投票
1 回答
768 浏览
提问于 2025-04-16 00:01

我想要一个类似下面的伪代码:

while input is not None and timer < 5:
    input = getChar()
    timer = time.time() - start

if timer >= 5:
    print "took too long"
else:
    print input

有没有办法做到这一点而不使用线程?我希望有一种输入方法,可以返回自上次调用以来输入的内容,如果没有输入的话,就返回Nonenull)。

1 个回答

4

在*nix系统上,你可以使用select配合sys.stdin来处理输入。而在Windows系统上,你需要用到msvcrt.kbhit()msvcrt.getch()这两个函数。

撰写回答