Python 当我试图结束一个不工作的程序时,不断给出 keyboardInterrupt,并且拆分功能不能正常工作

Python keeps giving keyboardInterrupt when I try to end a program that isn't working, and split function isn't working properly

我正在尝试制作一个 python 程序,它将根据给定的输入翻译成自定义语言。我让它与性别系统一起工作,例如 the man is de mno while the woman is 迪费里奥。然后我继续制作一个系统,其中 with the man 变成 aile mno 而不是 ail de mno .我不太擅长 Python,所以理论上我这样做应该可行。

我认为它的工作方式是介词通常出现在冠词和名词之前,因此如果翻译器遇到介词,它会提前检查名词以查看它是女性还是男性,并根据以下情况进行更改那(我成功做到了)。然后它将所有字符添加到 prepo。如果它命中 'the',则它会回头检查之前是否有介词。如果有,pass 函数 运行s 所以它不会为 'the' 添加任何翻译,但如果没有它会提前检查性别和 运行s和往常一样。

主要代码在elif word in Preposition:elif word == 'the':行。

最终目标是将 The man is with the woman 变成 De mno aili felio 而不是 De mno ail di felio.

我不知道代码是否真的有效,因为当我 运行 它并输入任何带有 'the' 的任何东西而不是 'the' 本身时,代码停止 运行ning 但我仍然可以输入内容并输入它们。当我结束程序时,它说 keyboardInterrupt.

我将不胜感激该问题的解决方案以及我将如何去做的解决方案。

import string

NounM = {
    'man': 'mno',
    'rock': 'lehr'
}

NounF = {
    'woman': 'felio',
    'chair': 'poen'
}

Verb = {
    'sit': 'colt',
    'is': 'ne',
    'are': 'sey',
    'was': 'sai'
}

Preposition = {
    'on': 'mit',
    'with': 'ail'
}

Pronoun = {
    'he': 'tse',
    'she': 'se',
    'i': 'ile',
    'me': 'men',
    'they': 'er',
    'it': 'ze'
}

Adjective = {
    'happy': 'kliony',
    'sad': 'probo',
    'good': 'klio',
    'bad': 'pro'
}

Article = {
    'that': 'arei',
    'those': 'sie'
}


def translate():
    sentence = input('Enter the sentence to turn into your custom language! ')
    split = sentence.split()
    translated_list = []
    translated_sentence = ''
    
    for index, word in enumerate(split):
        char = ''
        for a in string.punctuation:
            if str(a) in word:
                char = a
        if word in NounM:
            translated_sentence += NounM[word]
        elif word in NounF:
            translated_sentence += NounF[word]
        elif word in Verb:
            translated_sentence += Verb[word]
        elif word in Preposition:
            translated_sentence += Preposition[word]
            try:
                c = split[index + 1]
                while c not in NounM and c not in NounF:
                    a = 2
                    c = split[index + a]
                    a += 1
                if c in NounM:
                    translated_sentence += 'e'
                elif c in NounF:
                    translated_sentence += 'i'
            except IndexError:
                pass
        elif word in Pronoun:
            translated_sentence += Pronoun[word]
        elif word in Adjective:
            translated_sentence += Adjective[word]
        elif word in Article:
            translated_sentence += Article[word]
        elif word == 'the':
            try:
                c = split[index - 1]
                while c not in Preposition:
                    a = 2
                    c = split[index - a]
                    a += 1
                f = len(c)
                if c[f] == 'e' or c[f] == 'i':
                    pass
            except IndexError:
                c = split[index + 1]
                while c not in NounM and c not in NounF:
                    a = 2
                    c = split[index + a]
                    a += 1
                if c in NounM:
                    translated_sentence += 'de'
                elif c in NounF:
                    translated_sentence += 'di'
                else:
                    pass
        elif word == 'a':
            c = split[index + 1]
            while c not in NounM and c not in NounF:
                a = 2
                c = split[index + a]
                a += 1
            if c in NounM:
                translated_sentence += 'es'
            elif c in NounF:
                translated_sentence += 'en'
            else:
                pass
        else:
            translated_sentence += word
        word += str(char)
        word += ' '
        for i in translated_sentence:
            translated_list += i
        translated_list += str(char)
        translated_list += ' '
        translated_sentence = ''

    leng = len(translated_list) - 2
    final = translated_list[leng]
    if final in string.punctuation:
        translated_list.remove(final)

    translated_sentence = ''
    for i in translated_list:
        translated_sentence += i

    if final in string.punctuation:
        translated_sentence += final

    print(translated_sentence)
    other_translate = input('Would you like to translate another sentence? y/n ')
    if other_translate == 'y':
        translate()


translate()

此错误的原因是代码永远卡在 while 循环中。错误在这里:

while c not in Preposition:
    a = 2
    c = split[index - a]
    a += 1

如果输入的句子没有'on'或'with'。这个循环将永远迭代。

你应该为这个循环添加一个 break case,例如添加这些代码:

while c not in Preposition:
    a = 2
    c = split[index - a]
    a += 1
    if (index - a) < 0:
        break

希望有用!