2017-03-28 45 views
0

鑑於我構建的這個函數,我收到最後一個返回語句的錯誤,說我無法將它們結合在一起。有另一種方法可以做到這一點嗎?Python3無法連接錯誤?怎麼修?

def simple_pig_latin(input, sep=' ', end='.'): 
words=input.split(sep) 
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= ('!','@','#','$','%','^','&','*','.') 
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,'')#remove the 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= new_sentence + new_word+ sep 
    else: 
     new_sentence.append(word) 

return sep.join(new_sentence)+end 

回答

1

我認爲這是你在找什麼。 (您需要使用 ''。加入())

def simple_pig_latin(input, sep=' ', end='.'): 
    words=input.split(sep) 
    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= ('!','@','#','$','%','^','&','*','.') 
    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,'')#remove the 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= ''.join(new_sentence) + ''.join(new_word) + ''.join(sep) 
     else: 
      new_sentence.append(word) 

    "".join(new_sentence) 
    return new_sentence 




    print simple_pig_latin("welcome to the jungle.") # Sample call to func 

結果

lcomeway otay hetay ungle.jay 

編輯1

替代方法

def simple_pig_latin(input, sep=' ', end='.'): 
    words = input.split(sep) 
    sentence ="" 
    for word in words: 
     endString= str(word[1:]) 
     them=endString, str(word[0:1]), 'ay' 
     newWords="".join(them) 
     sentence = sentence + sep + newWords 
    sentence = sentence + end 
    return sentence 


    print simple_pig_latin("I like this ") 
+0

我不能使用print語句,只返回@Cyrus –

+0

對不起,打印在那裏進行調試。刪除它,請嘗試。它的作品(只要你期望的輸出是我所得到的(歡迎來到叢林中)。返回作爲elcomeway otay hetay ungle.jay – Cyrus

+0

jay前應該沒有點 –