Say I want to time how long I can hold my breath, and I want to do that with Python. I have short script:
start = time()
try:
while True: pass
except KeyboardInterrupt:
print(time() - start)
This has the basic functionality I want, but it has an fatal shortcoming. After a long period of holding my breath, my mind might be a little fuzzy, and I might not find the coordination to hit Ctrl+c right away, and I might loose important data about my training.
The spacebar is much easier target to hit. Is there a simple way to make the loop stop when I press it?
EDIT: I'm on OSX