Python错误:“字符串索引超出范围”

2024-03-29 00:47:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python制作了一个Pig拉丁语翻译程序(包含所有特定的规则),下面是我的代码:

print ("Enter some text here to be translated to Pig Latin: ");
text = input("> ");

wordlist = [];
letterlist = [];

for word in text:
    if word[0] != "a" and word[0] != "e" and word[0] != "i" and word[0] != "o" and word[0] != "u":
        if word[1] == "a" or word[1] == "e" or word[1] == "i" or word[1] == "o" or word[1] == "u":
            for number in range(1, len(word) - 1):
                letterlist.append(word[number]);
            letterlist.append(word[0]);
            letterlist.append("ay");
            new_word = "".join(letterlist);
            wordlist.append(new_word);
            letterlist = [];
        else:
            for number in range(2, len(word) - 2):
                letterlist.append(word[number]);
            letterlist.append(word[0]);
            letterlist.append(word[1]);
            letterlist.append("ay");
            new_word = "".join(letterlist);
            wordlist.append(new_word);
            letterlist = [];
    else:
        letterlist.append(word);
        letterlist.append("way");
        new_word = "".join(letterlist);
        wordlist.append(new_word);
        letterlist = [];

pigLatin = " ".join(wordlist);
print (pigLatin);

我得到一个指向线的错误:

if word[1] == "a" or word[1] == "e" or word[1] == "i" or word[1] == "o" or word[1] == "u":

说明字符串索引超出范围。请帮帮我!你知道吗


Tags: orandtextinnumbernewforif
1条回答
网友
1楼 · 发布于 2024-03-29 00:47:52

这里有几个问题。我将通过在代码中添加#注释来构建我的答案。你知道吗

一般来说,应该首先将字符串中的输入拆分为“单词”列表(不包括空格的字符组)。此外,在检查word[1]的值之前,应该添加一个类似if len(word) < 2:; continue的块,以便正确处理只有一个字符长的提交(例如字母“a”)。你知道吗

print ("Enter some text here to be translated to Pig Latin: ");
text = input("> ");

wordlist = [];
letterlist = [];

for word in text: # how do you know that text will be separated into words?
# as of right now, text is a string of characters, not a list of words. you 
# should use something like words = text.split(), and then iterate over words. 
# .split() converts a string into a list of strings. by default it separates
# according to the whitespace between characters.
    if word[0] != "a" and word[0] != "e" and word[0] != "i" and word[0] != "o" and word[0] != "u":
        if word[1] == "a" or word[1] == "e" or word[1] == "i" or word[1] == "o" or word[1] == "u": 
# here's the problem referenced in your question. 
# since text is a string of characters, when you iterate over it with `for`,
# you will be looking at one character at a time. each character has only one 
# index, 0. for example 'e'[0] will return 'e', but 'e'[1] will throw an error,
# since there is no index 1.  

            for number in range(1, len(word) - 1):
                letterlist.append(word[number]);
            letterlist.append(word[0]);
            letterlist.append("ay");
            new_word = "".join(letterlist);
            wordlist.append(new_word);
            letterlist = [];
        else:
            for number in range(2, len(word) - 2):
                letterlist.append(word[number]);
            letterlist.append(word[0]);
            letterlist.append(word[1]);
            letterlist.append("ay");
            new_word = "".join(letterlist);
            wordlist.append(new_word);
            letterlist = [];
    else:
        letterlist.append(word);
        letterlist.append("way");
        new_word = "".join(letterlist);
        wordlist.append(new_word);
        letterlist = [];

pigLatin = " ".join(wordlist);
print (pigLatin);

相关问题 更多 >