互动结束时通过按键打破循环

时间:2019-04-14 13:42:06

标签: python loops break

我想通过按键打破循环。但任何时候都不会。我希望循环的当前迭代结束,然后退出循环。

我发现的所有类似问题大部分都建议捕获KeyboardInterrupt,所以我希望这不会重复。

1 个答案:

答案 0 :(得分:0)

在迭代结束时,通过Polling the keyboard (detect a keypress) in python中列出的任何方法检查是否有按键,如果有,则退出循环。

例如with the cross-platform implementation of kbhit

while True:
    do_something()
    if kbhit(): break