我正在接受一个句子并将其变成猪拉丁语,但是当我编辑列表中的单词时,它永远不会停留。
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 :(得分:0)
因为你没有改变句子
所以要获得你想要的结果
:init
所以现在打印new_sentence
我把它设置为返回一个字符串,如果你想要一个可以轻松完成的列表
new_sentence = ''
for word in sentence:
if word[0] in "aeiou":
new_sentence += word +'yay' + ' '
else:
new_sentence += word + ' '
如果您正在使用列表,然后想要将列表转换为字符串,那么只需
new_sentence = []
for word in sentence:
if word[0] in "aeiou":
new_sentence.append(word + 'yay')
else:
new_sentence.append(word)
答案 1 :(得分: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循环的一些事情所以我没有使用它。遗憾
答案 2 :(得分: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)