I want to break out of a loop by a keystroke. But not at any time. I want the current iteration of the loop to finish up and then exit the loop.
All similar question I found mostly suggest catching KeyboardInterrupt, so I hope this is no duplicate.
I want to break out of a loop by a keystroke. But not at any time. I want the current iteration of the loop to finish up and then exit the loop.
All similar question I found mostly suggest catching KeyboardInterrupt, so I hope this is no duplicate.
At the end of an iteration, check if there was a keypress by any of the methods listed in Polling the keyboard (detect a keypress) in python and break out of the loop if there was.
E.g. with the cross-platform implementation of kbhit
:
while True:
do_something()
if kbhit(): break