I have a problem with breaking out of a loop by pressing a key.
I googled around and found msvcrt module but it did not solve my problem.
Here is my code.
while True:
"""some code"""
if *keyboard_input: space* == True:
break
I know it's a easy question but I just can't find the right module to import.
Thanks!
Use a try/except that intercepts KeyboardInterrupt:
while True:
try:
# some code
except KeyboardInterrupt:
print 'All done'
# If you actually want the program to exit
raise
Now you can break out of the loop using CTRL-C. If you want the program to keep running, don't include the raise statement on the final line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With