Python错误:";“字符串索引超出范围”;
我用python制作了一个Pig拉丁翻译程序(包含所有特定规则),下面是我的代码:Python错误:";“字符串索引超出范围”;,python,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[
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":
表示字符串索引超出范围。请帮忙 这里有几个问题。我将通过在代码中添加#注释来构建我的答案 一般来说,您应该首先将字符串中的输入拆分为“单词”列表(不包括空格的字符组)。此外,在检查单词[1]的值之前,应该添加一个块,如
if len(word)<2:;继续
,以便正确处理只有一个字符长的提交(例如字母“a”)
如果输入一个单字母单词,单词[1]将是什么?如何将两个恭维条件嵌套在一起?@jwodder忘记单字母输入,文本中单词的
行:
从字符串文本中提取一个字符。这对于多字母输入也是一个错误
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);