I started taking a "introduction to programming" class a month ago, and the introductory language we are using is Python. Because I have never programmed before and only have a month's worth of experience with Python, please forgive my mistakes.


So basically, here's the prerequisites to the "Piglatin Translator" assignment we were given:

1.) If a word has no letters, do not translate it.

2.) Punctuation and capitalization should be preserved.

3.) Separate each word into two parts. The first part is called the “prefix” and extends from the beginning of the word up to, but not including, the first vowel. (The letter “y” will be considered a vowel.) The Rest of the word is called the “stem.”
 
4.) The Pig Latin text is formed by reversing the order of the prefix and stem and adding the letters “ay” to the end. For example, “sandwich” is composed of “s” + “andwich” + “ay” + “.” and would translate to “andwichsay.”

5.) If the word contains no consonents, let the prefix be empty and the word be the stem. The word ending should be “yay” instead of merely “ay.” For example, “I” would be “Iyay.”

And finally, here's my code:

t = 0
again = "y"
consonants = "bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ"
while "y" in again:
    word = raw_input('\nPlease type a word to translate: ')
    while len(word) == 0:
        print "\nYou cannot translate a non-existant word!\n"
        word = raw_input('Please type a word to translate: ')
    for x in word:
            if (x == "a") or (x == "A") or (x == "e") or (x == "E") or (x == "i") or (x == "I") or (x == "o") or (x == "O") or (x == "u") or (x == "U") or (x == "y") or (x == "Y"):
                break
            t = t+1    
    prefix = word[:t]
    stem = word[t:]
    if consonants not in word:
        print "Translated word:",
        print word + "yay"
    else:
        print "Translated word:",
        print stem + prefix + "ay"
    again = raw_input("\nWould you like to translate another word into Piglatin? ")

I choose to splice words instead of split because it made more sense to me.
The program works correctly when the "if consonants not in word" statement is taken out,
but then that also makes #5 in the prerequisites not fulfilled.
When it is left in every translated word is out of order, and it basically just messes the process up.

Any ideas why?

Thanks in advance!

Consonants is one long string which is unlikely to be inside word. Maybe you meant set intersection or not any with generator expression?

Since words are not very long, it is not a problem to iterate through the entire word and keep track of the position of the first vowel, and if there is a consonant.

first_vowel = -1         ## location of first vowel
    consonant_found = False  ## assume no consonants
    t = 0
    for x in word:
        ## use lower() or upper() and one list of vowels
        if x.lower() in ["a", "e", "i", "o", "u", "y"]:
            if first_vowel == -1:     ## -1 == not found yet
                first_vowel = t
        else:
            consonant_found = True
        t += 1

    prefix = word[:first_vowel]
    stem = word[first_vowel:]
    if not consonant_found:
        print "Translated word:",
        print word + "yay"
    # etc.

"Yay" translates to "Yayyay"

Edited 5 Years Ago by woooee: n/a

Figured it out, thanks!

Edited 5 Years Ago by Tomie: n/a

This question has already been answered. Start a new discussion instead.