-2

I'm trying to create an application that takes a string and converts it into Pig Latin in Python. The code I have so far is this:

test = "hello world"
def PigLatin():
    split_test = test.split()
    for i in split_test:
        wordlist = list(i)
        wordlist.append(i[0])
        return wordlist
print PigLatin()

I'm attempting to take the first character of each word and append it to the end of said word. However, when I run the code it only edits "hello" or "world" depending on the location of the return statement. What am I doing wrong here? Any help would be greatly appreciated.

1 Answers1

0

The return statement causes your function to exit and hand back a value to its caller. So, return statement in your for loop sends a value back to the PigLatin function call, and is done, out of the stack. Also, read this please. Code:

test = "hello world"
def PigLatin():
    ret = []
    split_test = test.split()
    for i in split_test:
        wordlist = list(i)
        wordlist.append(i[0])
        ret.append(wordlist)
    return ret
print PigLatin()
Community
  • 1
  • 1
devautor
  • 2,506
  • 4
  • 21
  • 31
  • Thank you! I wasn't sure where the return statement should be, and I tried that location, but it was only returning the word "worldw". After trying out your code I realized that the empty list you created was necessary as well. – user7179971 Apr 13 '17 at 02:02
  • If the answer did help you, please accept it. Green motivates! :) – devautor Apr 13 '17 at 12:29