Quilty Kim
Quilty Kim

Reputation: 463

forloop in python pig latin translator

I've been trying to work on a pig latin translator in python and I had setup a forloop to loop through each character of a user-entered string. To test that the forloop is successfully catching all the consonants that follow the initial consonant until it hits a vowel(e.g. "str" in the word "string"), I had written a print statement to print out all of these consecutive consonants in a word that begins with a consonant. Unfortunately, the forloop only omits the letter "a" but allows the vowels "e" "i" "o" and "u" to be printed.

So my question here is how can I fix this code so that only the first string of consonants until the first vowel of the entered word?

I'm not particularly equipped with the language to describe the process by which I'm doing this but hopefully this plea is a close enough approximation that helps express the nature of the problem. Thanks.

code:

def translate():
     print("Welcome to the Pig Latin Translator")
     original=raw_input("What word would you like to translate?")
     length=len(original)
     move_letters=""
     index=0
     for i in range(length):
        if original[i]!=("a" or "e" or "i" or "o" or "u"):
            print(original[i])
            move_letters=move_letters+original[i]
            index+=1


translate()

Upvotes: 1

Views: 1029

Answers (1)

Mario Rossi
Mario Rossi

Reputation: 7799

The expression

("a" or "e" or "i" or "o" or "u")

is equivalent to "a". This is why your code is only catching "a". You need to write:

if not original[i] in ("a","e","i","o","u") :

or

if original[i] != "a" and original[i] != "e" and original[i] != "i" and original[i] != "o" and original[i] != "u" :

The reason why ("a" or "e" or "i" or "o" or "u") is equivalent to "a" is that, according to its definition, the or operator returns the first operand equivalent to true or 0 if there is none. In this case, '"a"' is equivalent to true, so it is the result of the whole expression.

You also need to add:

else:
    break

to the if.

Upvotes: 1

Related Questions