def pigWord(word): first = word[0] end = word[1:] sentence = "This is a test" word = sentence.split() finish = end + first + "ay" return finish print (pigWord)
Problem with Pig latin and word split
Page 1 of 14 Replies - 3886 Views - Last Post: 09 November 2012 - 07:30 AM
#1
Problem with Pig latin and word split
Posted 09 November 2012 - 01:47 AM
So I am trying to do a basic pig latin translator. I got it to do single words. But I cannot get the split to work. I have racked my brain and many web pages for hours trying to get the two to work together. Some help would be greatly appreciated.
Replies To: Problem with Pig latin and word split
#2
Re: Problem with Pig latin and word split
Posted 09 November 2012 - 02:29 AM
Two things:
First, you're printing the wrong thing.
will print out something like this:
That's just a function object. What you want is to print the result of calling the function with some parameter:
The other thing is that after you split sentence, and assign it to the variable word, you never use it again - so how can you tell that it's not working?
First, you're printing the wrong thing.
print (pigWord)
will print out something like this:
<function pigWord at 0x914fc34>
That's just a function object. What you want is to print the result of calling the function with some parameter:
print (pigWord("hey"))
The other thing is that after you split sentence, and assign it to the variable word, you never use it again - so how can you tell that it's not working?
#3
Re: Problem with Pig latin and word split
Posted 09 November 2012 - 02:49 AM
Yes I do understand that.
If I plug in
I get (his is a testTay).
I am trying to remove the first letter from each word and move it the the end of each word and add "ay".
My online book does not go into much of any detail on how to use split. I have tried so many different possibilities and I keep getting the same results. Should I make another function for split?
If I plug in
print(pigWord("this is a test"))
I get (his is a testTay).
I am trying to remove the first letter from each word and move it the the end of each word and add "ay".
My online book does not go into much of any detail on how to use split. I have tried so many different possibilities and I keep getting the same results. Should I make another function for split?
#4
Re: Problem with Pig latin and word split
Posted 09 November 2012 - 02:58 AM
Nope, you're actually nearly there. As I said, you split up the string, but you never do anything with the result.
This will put an extra space at the end of the line, but that's easy to fix
def pigWord(word): finish = "" # Split the sentence into words words = word.split() for w in words: # Make the piglatin word first = w[0] end = w[1:] finish += end + first + "ay " # Append it to the new string return finish print (pigWord("Test with several words"))
This will put an extra space at the end of the line, but that's easy to fix

#5
Re: Problem with Pig latin and word split
Posted 09 November 2012 - 07:30 AM
oh man now I feel dorky. I should have realized that I needed a for loop to apply it to each word.
Thank you so much.
Thank you so much.
Page 1 of 1