2016-06-14 52 views
0

我正在服用一個句子並將它變成拉丁語,但是當我編輯列表中的單詞時,它永遠不會停留。需要編輯for循環中的列表。 [Python}

sentence = input("Enter a sentence you want to convert to pig latin") 

sentence = sentence.split() 
for words in sentence: 
    if words[0] in "aeiou": 
     words = words+'yay' 

當我打印句話我得到我放在同一個句子。

回答

0

另一種方式來做到這一點(包括一些修正)

sentence = input("Enter a sentence you want to convert to pig latin: ") 

sentence = sentence.split() 
for i in range(len(sentence)): 
    if sentence[i][0] in "aeiou": 
     sentence[i] = sentence[i] + 'yay' 
sentence = ' '.join(sentence) 

print(sentence) 
0

因爲你沒有改變句子

因此,爲了得到你想要的

new_sentence = '' 
for word in sentence: 
    if word[0] in "aeiou": 
     new_sentence += word +'yay' + ' ' 
    else: 
     new_sentence += word + ' ' 

所以結果現在打印new_sentence

我設置它返回一個字符串,如果你會rath呃具有可如果您使用的是列表的工作那樣容易

new_sentence = [] 
for word in sentence: 
    if word[0] in "aeiou": 
     new_sentence.append(word + 'yay') 
    else: 
     new_sentence.append(word) 

來完成的列表,你想那麼列表轉換爲字符串然後就

" ".join(new_sentence) 
0

這似乎並不彷彿你正在更新句子。

sentence = input("Enter a sentence you want to convert to pig latin") 
sentence = sentence.split() 
# lambda and mapping instead of a loop 
sentence = list(map(lambda word: word+'yay' if word[0] in 'aeiou' else word, sentence)) 
# instead of printing a list, print the sentence 
sentence = ' '.join(sentence) 
print(sentence) 

PS。有點忘了一些Python的for循環的東西,所以我沒有使用它。對不起