KeyboardInterrupt handeling with sys.exit() not working on pyCharm

54 Views Asked by At

I am a beginner python learner, and i am learning from a book a small animated project called ZigZag. IDE is Pycharm and i am using Try and Except KeyboardInterrupt and the code for the Except clause is sys.exit() ; basically, if I type CTRL+C in my keyboard while the program is running, the animation and the program stops and the program quit, BUT IT DOES NOT. Been 48hrs trying to solve this and zero result. Would mean a lot to me if you help. I can consider this issue as my 1st main major problem occured to me in my programming journey (complete beginner).

This is basically the code writing in Pycharm, when i run it; and try to input CTRL+C , the program doesn't apply sys.exit() and quit.

import sys, time

indent = 0
indentIncreasing = True

try:

    while True:
        print(' ' * indent, end='')
        print('********')
        time.sleep(0.1)

        if indentIncreasing:
            indent = indent + 1
            if indent == 15:
                indentIncreasing = False
        else:
            indent = indent - 1
            if indent == 0:
                indentIncreasing = True
except KeyboardInterrupt:
    sys.exit()
1

There are 1 best solutions below

0
Lars Frölich On

If you want your python script's output to run in a console you can interact with (for example to enter user input like Ctrl+C) you have to modify the run configuration. You can reach the run configuration either through the dropdown next to the "run" button, or by clicking on the wrench-icon in the output-window:

run configuration

Finally, in the run configuration window, under "execution" select "Emulate terminal in output console"

enter image description here