I am trying to write a program in Python that converts user input from English to Pig Latin ("Y" is not a vowel in this case, breaks off at first vowel and moves preceding consonants to the end). The end product of my program should be able to compute whole sentences, but right now im just trying to get it to do one word. This is what I currently have but whenever a string is inputted that does not begin with a vowel it goes through and performs the function for every instance of a vowel. For example:
Enter a word:hello
ellohay
ohellay

Please help?!

defining variables to be used later on

pyg = 'ay'
vowels = "aeiou"

get user input

original = raw_input('Enter a word:')

def main():

# make sure only alphabetic word is entered
if len(original) > 0 and original.isalpha():
    word = original.lower() # change to lower case input

    for word in original.split():
        if word[0] in vowels: # find if first letter is vowel
            new_word = word + pyg
            print new_word
        else:
            for i in range(1, len(word)): # search word for first instance of vowel
                if word[i] in vowels:
                    # splice word so that the consonants preceding the first vowel move to the end
                    new_word = word[i:] + word[:i] + pyg
                    print new_word
else: # if input is not alphabetic
    print 'That is not a word!'

main()

ok i figured out how to get it to stop after one by adding a while loop:

else:
                counter = 0
                while counter == 0:
                    for i in range(1, len(word)): # search word for first instance of vowel
                        if word[i] in vowels:
                            # splice word so that the consonants preceding the first vowel move to the end
                            new_word = word[i:] + word[:i] + pyg
                            print new_word
                            counter = counter + 1
                        else:
                            return

but now i have no clue of how to repeat this for every word in a sentence. i know how to use the split function, but i dont know how to call upon every word that is outputted after a split. i.e.

string = "hello world"
string.split()
>>> [hello, world]

right?

you can repeat your code for every word in string.split then join them at the end.

This article has been dead for over six months. Start a new discussion instead.