我想通过按键打破循环。但任何时候都不会。我希望循环的当前迭代结束,然后退出循环。
我发现的所有类似问题大部分都建议捕获KeyboardInterrupt,所以我希望这不会重复。
答案 0 :(得分:0)
在迭代结束时,通过Polling the keyboard (detect a keypress) in python中列出的任何方法检查是否有按键,如果有,则退出循环。
例如with the cross-platform implementation of kbhit
:
while True:
do_something()
if kbhit(): break