How to stop Python while running?
Sebastian Wright
I'm going to the programming competition and I need help. How do I stop a program while code is running? I tried the sys. exit () or only exit () but it doesn't work as I imagined. Here's the beginning of the code, so if anyone can help me, please answer. Thanks in future. Here is beginning of code: K = Int (input ()) J = Int (input ())
If K is < 9 or K, 11: Exit ()
If K is < 14 or K, 16: Exit ()
21 Answer
The code in your question has numerous errors, so let's start with Python code that prints the numbers between 1 and 10 at 5 second intervals between printing each number and then exits.
import time
i = 1
while True: print(i) i = i + 1 time.sleep(5) if(i > 10): break The above code will take almost a minute to finish executing unless it is interrupted before it is finished. To exit from a program before it is finished press the key combination Ctrl + C
Results of pressing Ctrl + C:
^CTraceback (most recent call last): File "print-1-to-10.py", line 6, in <module> time.sleep(5)
KeyboardInterrupt
``