0

I'm taking a sentence and turning it into pig latin, but when I edit the words in the list it never stays.

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'

And when I print sentence I get the same sentence I put in.

Hannan Rhodes
  • 123
  • 1
  • 2
  • 10

3 Answers3

0

Because you did not change sentence

So to get the results you want

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

So now print new_sentence

I set this up to return a string, if you would rather have a list that can be accomplished as easily

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

If you are working with a list and you want to then convert the list to a string then just

" ".join(new_sentence)
PyNEwbie
  • 4,882
  • 4
  • 38
  • 86
0

It does not seem as though you are updating sentence.

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 forgot some things about Python's for loop so I didn't use it. Sorry

Buckeye14Guy
  • 831
  • 6
  • 12
0

another way to do it (includes some fixes)

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)
Hans
  • 2,354
  • 3
  • 25
  • 35