Does it bother anyone else that this program doesn't take into account vowels at the front of the word?


In actual "pig latin" if a word starts with a vowel, you don't use the syntax that gets you past this course.

As such I wrote the following:

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    if word[0]!="a" or "e" or "i" or "o"or "u":
    if word [0]=="a" or "e" or "i" or "o" or "u":
    print new_word
    print 'empty'

In reality this would be a lot more complicated because of words like "Charlie" for example, which would actually be "Arliechay" - has anyone tried writing a program that looks to the word[0] and [1] and paired them if they are both consonants?


In theory, you could use something like a loop on the string until you come to a letter that's a vowel, then take the substring containing the consonants and move it to the end of the string. (Then, of course, add the 'ay')


I agree the most efficient way to do this would write a loop that ends when you come to a vowel. Such a good point though and I wish the course could get extended with this idea. Great stuff.


Although it probably isn't the most efficient thing in the world, I have written a program that does what you want. It accounts for all of the consonants at the start of the word. Go into IDLE or the codecademy text editor if you want to run it:

pyg = 'ay'

original = input('Enter a word: ')
vowels = ['a', 'e', 'i', 'o', 'u']

if len(original) > 0 and original.isalpha():
    word = original.lower()
    for x in range(len(word)-1):
        if word[x] not in vowels:
            if x == 0:
                new_word = word+'ay'
                first = word[0:x]
                new_word = word[x::]+first+'ay'

Let me know if you want me to explain any of it!

(@oscartbeamish the code may interest you too if you want to take a look at it :slight_smile: )


Very nicely done, I was just about to upload my version but it is nearly exactly the same as yours. I like the way you keep it old school by using "x" rather than "i" :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.