Search code examples
pythonkeyboardinterrupt

Why does using Ctrl+C keyboard interrupt crashes the code?


I just used Ctrl+C while writing my code to copy and paste some code. But after that, my code stopped running due to a keyboard interrupt. My code is compiled and has no errors. I tried it another time without Ctrl+C in a different file but it shows the same error.

filename = input('Enter Project Name: ')
codeLine = input(filename + '>')
varDict = {}
varList = []
constDict = {}
constList = []
codeLine = codeLine.strip()
codeLineList = codeLine.split()
while True:
    if codeLine.endswith(";"):
        codeLine = codeLine.rstrip(codeLine[-1])
        if len(codeLineList) == 2:
            if codeLineList[1] == "is":
                varName = (codeLine.split("is")[0]).strip()
                varValue = (codeLine.split("is")[1]).strip()
                varDict[varName] = varValue
                print(varDict)

And I am getting the following result.

Enter Project Name: hi
hi>i is k;

Nothing seems to happen after that. When I debugged the code, I found this error:


    Traceback (most recent call last):
      File "C:/Users/ajgameboy/PycharmProjects/mylang/main.py", line 9, in <module>
        codeLine = input(fileName + ">")
    KeyboardInterrupt
    Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)


Solution

  • Clicking CTRL-C in Python raises a KeyboardInterrupt exception. This is a way for the user to manually raise an error and interrupt the program. If you don't want this to happen you'll have to copy and paste without using CTRL-C.