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


#1

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():
    word=original.lower()
    if word[0]!="a" or "e" or "i" or "o"or "u":
        first=word[0]
        new_word=word[1:]+first+pyg
    if word [0]=="a" or "e" or "i" or "o" or "u":
        new_word=word+pyg
    print new_word
else:
    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?


#2

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')


#3

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.


#4

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:
            continue
        else:
            if x == 0:
                new_word = word+'ay'
                break
            else:
                first = word[0:x]
                new_word = word[x::]+first+'ay'
                break
    print(new_word)
else:
    print('empty')

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: )


#5

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:


#6

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