键盘中断使我的Python代码崩溃

问题描述 投票:0回答:2

我是一名新程序员,尽管我在编写代码时只是使用 ctrl c 来复制粘贴一些代码。但在那之后,我的代码由于键盘中断而停止运行。我的代码已编译并且没有错误。请帮忙。我在另一个文件中没有 ctrl c 的情况下又尝试了一次,但它显示了相同的错误。

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)

我得到以下结果。

Enter Project Name: hi
hi>i is k;

此后似乎什么也没发生。当我调试代码时,我发现了这个错误:


    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)

python keyboard interrupt
2个回答
0
投票

在 Python 中单击 CTRL-C 会引发键盘中断异常。这是用户手动引发错误并中断程序的一种方法。如果您不希望发生这种情况,则必须在不使用 CTRL-C 的情况下进行复制和粘贴。


0
投票

Ctrl + C 中断代码,至少在 google colab 中是这样

© www.soinside.com 2019 - 2024. All rights reserved.