2017-03-27 84 views
-1

我建立了這個函數來改變一個字符串輸入到拉丁語中。我試圖檢查索引是否超出範圍,但是我的檢查方法是讓索引超出範圍。我試圖檢查索引是否超出範圍,但我使用的代碼是索引超出範圍?

見如下:

def simple_pig_latin(input, sep=' ', end='.'): 
    words=input.replace(" ", " ").split(sep) 
    new_sentence="" 
    Vowels= ('a','e','i','o','u') 
    Digit= (0,1,2,3,4,5,6,7,8,9) 
    cons=('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z') 
    characters= ('!','@','#','$','%','^','&','*','.') 
    for word in words: 
     if word[0] == " ": 
      new_word=word 
     else: 
      if word[0] in Vowels: 
       new_word= word+"way" 
      if word[0] in Digit: 
       new_word= word 
      if word[0] in cons: 
       first_letter=word[0] #saves the first letter 
       change= str(word) #change to string      
       rem= change.replace(first_letter,'') 
       put_last= rem+first_letter #add letter to end 
       new_word= put_last+"ay" 
      if word[0] in characters: 
       new_word= word 
      new_sentence= new_sentence+new_word+sep 

new_sentence= new_sentence.strip(sep)+end 
return new_sentence 

你可以看到第一個if語句檢查,如果它是空的,但我得到這個確切的錯誤:

「9號線IndexError:字符串索引出的範圍「

我還可以檢查空序列嗎?我不能用範圍內的單詞(len(words)),因爲那麼我的if語句都不起作用。它會告訴我對象不是可下載的。

+1

「你可以看到第一個if語句是檢查它是否爲空「 - 不,它不是。它試圖訪問「word [0]」,看看「word [0]」是否是一個空格。它假定存在「word [0]」。這不像檢查'word'是否爲空。 – user2357112

+0

這個問題被低估的原因是什麼?我想解決我的錯誤,以便它不會得到可憐的選票。 –

回答

1
在循環

你認爲word不是空的,這是根本保證,因爲你根據空間分裂,而當有超過1個空間空字段可以被髮出。

>>> "a b".split(" ") 
['a', '', 'b'] 

所以,你可以使用split()不帶任何參數(僅適用於類空字符),或者如果你使用一些其他的分隔符,你的循環之前過濾掉空字段,例如在一個列表理解:

words= [ w for w in input.split(sep) if w ] 

現在您確定words的每個條目都至少有1個字符。

編輯:這麼多關於split並過濾掉空字符串漂亮的解釋,但似乎並沒有削減它,因爲你可以使用l作爲分隔符hello world,所以迴歸本源:

def simple_pig_latin(input, sep=' ', end='.'): 
    words=input.split(sep) 
    new_sentence="" 
    Vowels= ('a','e','i','o','u') 
    Digit= (0,1,2,3,4,5,6,7,8,9) 
    cons=set(('b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z')) 
    characters= ('!','@','#','$','%','^','&','*','.') 
    new_sentence = [] 

    for word in words: 
     if word: 
      if word[0] == " ": 
       new_word=word 
      else: 
       if word[0] in Vowels: 
        new_word= word+"way" 
       elif word[0] in Digit: 
        new_word= word 
       elif word[0] in cons: 
        first_letter=word[0] #saves the first letter 
        change= str(word) #change to string 
        rem= change.replace(first_letter,'') 
        put_last= rem+first_letter #add letter to end 
        new_word= put_last+"ay" 
       elif word[0] in characters: 
        new_word= word 
       new_sentence.append(new_word) 
     else: 
      new_sentence.append(word) 

    return sep.join(new_sentence)+end 

更改您的代碼:

  • 使用清單,並加入背後到底使用join
  • 只是篩選出一句空話,而是把它在名單反正
  • 更快的查找

現在很多使用set爲consomns的elif代替if

  • print(simple_pig_latin("hello world",sep='l')) 
    

    產量:

    ehayllo worwaylday. 
    
  • +0

    解決了超出範圍的錯誤!我仍然失敗這個測試用例:AssertionError:'ehaylo worwaylday'!='ehayllo worwaylday'我知道這與分隔符=「l」有關。如果我帶走了問題,我就會失敗所有的測試用例,那麼我該如何保留這個額外的l? –

    +0

    它告訴我,我不能連接最終返回statement.TypeError:只能連接列表(不是「str」)列表 –

    +0

    我測試了。所以也許你只是重新輸入我的解決方案(用錯字)而不是複製/粘貼。 –