2015-06-14 19 views
0

這是我必須製作豬拉丁語翻譯器的代碼,我似乎無法將其翻譯出來,任何人都有提示使其生效?Python中的程序

我認爲我的問題是在編碼內,並以元音部分開始,但我似乎無法弄清楚這一點。

+1

函數只處理第一個單詞然後返回。您應該創建一個空列表pattern = [],然後爲每個單詞調用pattern.append(word [1:] + word [0] +'ar')或pattern.append(word +'way'),然後返回pattern等結束了 – samgak

回答

0

調整這3個功能:

def starts_with_vowel(word): 
    # return True if the word starts with a vowel and False otherwise 
    return word[0] in ['a', 'e', 'i', 'o', 'u'] 

def encode(word): 
    # translate a single word to the secret language 
    # call starts with vowel to decide which pattern to follow 
    if starts_with_vowel(word): 
     return word[1:] + word[0] + 'ar' 
    else: 
     return word + 'way' 

def translate(message): 
    # translate the whole text to the secret language 
    # call encode to translate individual words in text 
    return ' '.join(encode(word) for word in message) 

最大的問題是encode()starts_with_vowel()通過所有單詞迭代(但您的評論說,它應該在一個字工作)的編碼

2

你忘了translate功能分配:
必須是:

phrase = ' '.join(encode(message)) 
return phrase 
2

除了@ delimitry的回答,也改變了在第二個功能,即如果條件,改變 -

if starts_with_vowel(words): 

到 -

if starts_with_vowel(word):