14 Replies - 21643 Views - Last Post: 28 September 2012 - 11:04 AM Rate Topic: -----

#1 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Simple pig latin program, having trouble.

Posted 27 September 2012 - 08:20 PM

Hello all. I'm trying to make an english to pig latin translating program. The rules are:
Find the first vowel in the English word, take the consonant(s) preceding that vowel, and move them to the end of the word, followed by “ay”.
If the first letter of the word is a vowel, then just add “ay” to the end.

Treat “Y” as a consonant.

For words with no vowels besides “Y” such as “my”, just print out the original word.

The capitalization should be preserved (assume that only the first letter of any word might be capitalized).

You can assume that the string has only letters and spaces with no punctuation.


So I came up with something that works for words that start with a vowel and words that don't. However, I can't seem to figure out how to print just the original word if no vowels were in that word. I also can't figure out how to have the program work properly if more than one word is entered. I've been messing around with the .split function. I'm guessing I would have to have a for loop that goes through the string and gets each word, append each of those words into a list, and then have another for loop that does the translating on each word?

Here's what I have so far, and thanks in advance for any help!


VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")

def main():

    word = raw_input("Please enter a word: ")

    if word[0] in VOWELS:
        print word + "ay"
    else:
        print word[1:] + word[0] +"ay"
main()




Is This A Good Question/Topic? 0
  • +

Replies To: Simple pig latin program, having trouble.

#2 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 08:36 PM

Update: Alright I figured out how to translate more than one word, however it prints each word like five times.

    statement = raw_input("Please enter a word: ")

    words =  statement.split()

    for word in words:
        for i in word:
            if word[0] in VOWELS:
                print word + "ay"
            else: print word[1:] + word[0] + "ay"


Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 09:00 PM

You are looping "i" times for each word, I think the solution you are looking for would just be to delete for i in words:
VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")

statement = raw_input("Please enter a word: ")

words =  statement.split()

for word in words:
        if word[0] in VOWELS:
            print word + "ay",
        else: print word[1:] + word[0] + "ay",

And put a comma at the end of each line so it doesn't print on a new line!
>> Please enter a word: Python is actually great
ythonPay isay actuallyay reatgay



To check there are no vowels in the whole word you will have to loop through each character checking:
  
for i in range(len(word)):
  if word[i] not in VOWELS 
  ...


And perhaps keep a count of how many non-vowels you see and compare that with the original length of the word.

This post has been edited by Simown: 27 September 2012 - 09:18 PM

Was This Post Helpful? 1
  • +
  • -

#4 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 09:21 PM

Wow that's a lot easier than what I was trying haha. I had two for loops running and appending stuff into another list then trying to print it all :rolleyes2: Thank you very much! Now I just need to figure out how to make it so if someone enters a word without a vowel (like my) it just prints that word. Also, I noticed that for words where the second letter is a consonant, it prints out starting from that letter. Example:
I enter in "Computer science is cool."

It displays as "omputerCay ciencesay isay oolcay" instead of "Omputercay iencescay isay oolcay". I realize it's because I have word[1:] (the second letter to the end) plus word[0]. Is there a way to search through the word to find the first vowel?

Woops, didn't see your last edit before I posted. Alright let me try a couple things out.
Was This Post Helpful? 0
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 09:28 PM

(I made some edits to the post before, so go back and read it if you haven't ;) Yeah, you saw :) Ignore this post for now!)

It's slightly trickier but very possible to find the first vowel, all you need to do is index the string (and I made it a function so it's easier):

def find_vowel(word):
    # This is from 0 to the length of the word
    for i in range(len(word)):
       if word[i] in VOWELS: 
         return i
    # Return an error value if there are not any (which solves another problem too!)
    return -1



This function finds the first vowel in the word. Can you take it from there? I am guessing this is homework so I'll try not to do too much. When you have returned the i, you want to slice the string from i and not just from 1.

This post has been edited by Simown: 27 September 2012 - 09:34 PM

Was This Post Helpful? 1
  • +
  • -

#6 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 09:41 PM

Alrighty I made some changes:


    statement = raw_input("Please enter a word: ")

    words =  statement.split()
    count = 0

    for word in words:
        for i in range(len(word)):
            if word[i] not in VOWELS:
                count += 1
                if count == len(word):
                    print word,
            elif word[0] in VOWELS:
                print word + "ay",
            else:
                print word[1:] + word[0] + "ay",



So it prints words like "my" properly, but if it's another word it prints it multiple times for some reason.
Was This Post Helpful? 0
  • +
  • -

#7 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 10:20 PM

May I suggest you use the function to find the fist vowel that I just suggested? Otherwise you are going to have to search through the words twice once for "non-vowels" and another time to reverse it. Here's what I have, basically your code tidied up with the new function:

def find_vowel(word):
    # This is from 0 to the length of the word
    for i in range(len(word)):
       if word[i] in VOWELS: 
         return i
    # Return an error value if there are not any (which solves another problem too!)
    return -1

VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")

statement = raw_input("Please enter a word: ")

words =  statement.split()
count = 0

for word in words:
  vowel = find_vowel(word)
  # No vowels found
  if(vowel == -1):
    print word,
  # A vowel is the first letter
  elif(vowel == 0):
    print word + "ay",
  else:
    # See how I split from the position of the vowel?
    print word[vowel:] + word[:vowel] + "ay",
    
>>> Please enter a word: My science is explosive
My iencescay isay explosiveay



Hope that helps :)

This post has been edited by Simown: 27 September 2012 - 10:23 PM

Was This Post Helpful? 1
  • +
  • -

#8 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 10:49 PM

That helps a lot! Thank you very much I appreciate it! Unfortunately I'm not allowed to use methods yet, but I think I can figure it out now that I understand the gist of what I have to do (or at least I hope so). I shall be reporting back if I get it working!
Was This Post Helpful? 0
  • +
  • -

#9 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 10:52 PM

Ok, please do!

P.S. You do actually have a main() method already, but I guess you are allowed that ;)

You can use built-in methods though? string.find() might be helpful to find the vowels in that case.

This post has been edited by Simown: 27 September 2012 - 10:53 PM

Was This Post Helpful? 1
  • +
  • -

#10 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 11:24 PM

Bleh this is a lot harder without being able to use that function. I can use all the string methods though, however I've never used the .find() before. Would I do something like string.find(VOWEL, word) ?

I received this error:

Traceback (most recent call last):
File "hw3.py", line 53, in <module>
main()
File "hw3.py", line 32, in main
vowel = string.find(VOWELS, word)
File "/usr/lib/python2.6/string.py", line 361, in find
return s.find(*args)
AttributeError: 'tuple' object has no attribute 'find'
Was This Post Helpful? 0
  • +
  • -

#11 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 11:30 PM

You can only give a single argument to find() not a list of arguments (VOWELS)

Instead of using that function you have to save the vowel, testing through all of them:

for word in words:
    for vowel in VOWELS:
        if vowel in word:
            # Find the first vowel that comes up
            i = word.find(vowel)
            #Found the first one, stop there!
            break
        else:
            # Set the error value again
            i = -1



Or another, similar way with slightly less tests:

for word in words:
    for vowel in VOWELS:
        i = word.find(vowel)
        if(i != -1):
           break


This post has been edited by Simown: 27 September 2012 - 11:35 PM

Was This Post Helpful? 1
  • +
  • -

#12 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 11:40 PM

I'm sorry, not allowed to use break either :( I really do understand how it's supposed to work now, but having to abide by all these rules makes it harder. If I was allowed to use a method from earlier it would be so much easier.

Edit:

I guess I could use boolean though instead of the break?
Was This Post Helpful? 0
  • +
  • -

#13 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple pig latin program, having trouble.

Posted 27 September 2012 - 11:59 PM

Not allowed to use break? But it's a standard programming construct!

Well I guess you could see if it hasn't already been set, like this:

# Set a default value, the error value
first = -1
for word in words:
    for vowel in VOWELS:
        j = word.find(vowel)
        # If i hasn't been set and j is found
        if(first != -1) and (j != -1):
         # Now "first" is the first index of the first vowel found, and won't be set again
         first = j



It's a little wasteful honestly because it keeps going if it matches the first one until all of the vowels have been compared but as you say, you can't "break" out of the loop.

Boolean would work equally well I suppose, but this way "first" is the actual index and not just "True" or "False"

Edit: Changed code a little

This post has been edited by Simown: 28 September 2012 - 12:14 AM

Was This Post Helpful? 1
  • +
  • -

#14 blindchicken11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 19-September 12

Re: Simple pig latin program, having trouble.

Posted 28 September 2012 - 10:04 AM

Good news! I convinced my prof. to let me use methods :bigsmile: Makes this so much easier now. I got it figured out thanks to your help! I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#15 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple pig latin program, having trouble.

Posted 28 September 2012 - 11:04 AM

That's great! Happy to help, and best of luck on the result :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1