4 Replies - 2467 Views - Last Post: 09 November 2012 - 07:30 AM Rate Topic: -----

#1 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-October 12

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.

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)



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Pig latin and word split

#2 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Problem with Pig latin and word split

Posted 09 November 2012 - 02:29 AM

Two things:

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?
Was This Post Helpful? 0
  • +
  • -

#3 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-October 12

Re: Problem with Pig latin and word split

Posted 09 November 2012 - 02:49 AM

Yes I do understand that.
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?
Was This Post Helpful? 0
  • +
  • -

#4 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

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.
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 ;)
Was This Post Helpful? 0
  • +
  • -

#5 SuperSlow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-October 12

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1