Reputation: 11
Trying to build a super basic pig latin translator.
The thought process was to go with one word first, get every letter except the first, then concatenate back on the first letter and the “ay.” This worked (yay).
Moving onto phrases, I figured I could split the phrase into a list and the for loop would iterate over that list and perform that function on each word. Instead I keep ending up with “ig latinpay,” which is tragically wrong.
def translate(phrase):
translation = ""
text = phrase.split(" ")
for word in text:
cut_word = phrase[1:len(phrase)]
translation = cut_word + phrase[0] + "ay"
return translation
print(translate(input("Enter a word: ")))
There are similar questions on here but my confusion is specifically around why the for loop isn't iterating over each word (which I think should exist as a list once the phrase is split). Thanks in advance for any guidance!
P.S. I know I need to add an if for vowels, but trying to get through this fundamental error first
Upvotes: 1
Views: 161
Reputation: 2915
There are two things tripping this up.
Inside your for loop, rather than operating on the word, you are still operating on the phrase as a whole.
When you perform that operation, rather than adding to translation
, you're assigning to it, which overwrites the previous value.
You'll have to fix both of those for this code to perform as expected.
Upvotes: 2