热门Python输入循环
我想要一个类似下面的伪代码:
while input is not None and timer < 5:
input = getChar()
timer = time.time() - start
if timer >= 5:
print "took too long"
else:
print input
有没有办法做到这一点而不使用线程?我希望有一种输入方法,可以返回自上次调用以来输入的内容,如果没有输入的话,就返回None
(null
)。
1 个回答
4
在*nix系统上,你可以使用select
配合sys.stdin
来处理输入。而在Windows系统上,你需要用到msvcrt.kbhit()
和msvcrt.getch()
这两个函数。