2016-06-14 17 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を印刷してください

私はこれを設定して文字列を返します。えーリストで作業しているかのように簡単に

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。 KindaはPythonのforループに関するいくつかのことを忘れてしまったので、私はそれを使用しませんでした。申し訳ありません

関連する問題