samedi 18 février 2017

Python Pig Latin Translator with multiple consonants

I'm working on a "Pig Latin" translator, but it's a bit different than the ones addressed so far. In addition to vowel adding a "yay" at the end and consonants being removed from beginning and added to the end with "ay", it also needs to adjust to allow for multiple consonants words and have all consonants moved to the end, followed by the "ay". So "schmooze" would be "oozeschmay" (Y is treated as a consonant) (The error messages that I've included are required)

Here is my code, it sorta works. By sorta I mean that it gives back too many answers and I don't know how to make it not fullfill all requirments with consonants.

For example, with the word "yellow" it give back: ellowyay llowyeay lowyelay owyellay

print("Hey Nerds, Welcome to the Pig Latin Translator!")
print("Please enter ONE word. Do not use punctuation or numbers- just your regular ol' alphabet")
pygCons = 'ay'
pygVowel = "yay"
wordError = "Error, you stinker! Please enter ONE word without punctuation."

original = input("Enter a word:")

if len(original) > 0 and original.isalpha():
    word = original.format()
    first = word[0]
    second = word[:2]
    third = word[:3]
    fourth = word[:4]
    if first in 'aeiou':
        newWord = word + pygVowel
        print(newWord)     
    if first not in 'aeiou':
        newWord = word[1:] + first + pygCons
        print(newWord)
        if second not in 'aeiou':
            newWord = word[2:] + second + pygCons
            print(newWord)
            if third not in 'aeiou':
                newWord = word[3:] + third + pygCons
                print(newWord)
                if fourth not in 'aeiou':
                    newWord = word[4:] + fourth + pygCons
                    print(newWord)
                else:
                    print("That's not a real word")
else:
    print(wordError)

Aucun commentaire:

Enregistrer un commentaire