-1
我正在Python中的豬拉丁語翻譯(包括所有的具體規則),並在這裏是我的代碼:Python的錯誤:「字符串索引超出範圍」
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]是什麼? – jwodder 2015-01-26 19:06:32
你怎樣才能在彼此中嵌套兩種恭維條件? – ZdaR 2015-01-26 19:07:44
@jwodder忘記1個字母的輸入,'for text in text:'行'從字符串'text'中提取1個字符。所以這對於多字母輸入也是一個錯誤 – 2015-01-26 19:09:04