2015-01-26 80 views
-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": 

表示字符串索引超出範圍。請幫忙!

+3

如果輸入單字母單詞,單詞[1]是什麼? – jwodder 2015-01-26 19:06:32

+0

你怎樣才能在彼此中嵌套兩種恭維條件? – ZdaR 2015-01-26 19:07:44

+1

@jwodder忘記1個字母的輸入,'for text in text:'行'從字符串'text'中提取1個字符。所以這對於多字母輸入也是一個錯誤 – 2015-01-26 19:09:04

回答

1

這裏有幾個問題。我將通過在您的代碼中添加#註釋來構建我的答案。

一般來說,你應該首先將輸入從一串字符分割成一個「單詞」(不包括空格的字符組)列表。另外,在檢查單詞[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);