Laura Frost
Laura Frost

Reputation: 13

Write a Python program that requests a word (in lowercase letters) as input and translates the word into Pig Latin

I need to Write a Python program that requests a word (in lowercase letters) as input and translates the word into Pig Latin. • The rules for translating a word into Pig Latin are as follows: a) If the word begins with a vowel, add way to the end of the word. For instance, else becomes elseway. b) If the word begins with a group of consonants, move them to the end of the word and add ay. For instance, chip becomes ipchay.

My current Coding:

word = input("Enter word to translate: ")
#if the 1st letter of a word is "aeiou", add "way" to the end of the word 
if word[0] == "a" or "e" or "i" or "o" or "u":  
    print(word + "way")
elif word[0] and word[1] == "b" or "c" or "d" or "f" or "g" or "h" or "j" or "k" or "l" or "m" or "n" or "p" or "q" or "r" or "s" or "t" or "v" or "x" or "y" or "z":
    print(word + "ay")

It seems that my current coding has some problem as it only displays words with adding "ways" to the end of word regardless of whether the first letter of the word is vowel or consonant. Also, I am not sure how to move the first letter of a word to the end of the word if the it is a group of consonant and do not know how to deal with if the first letter and second letter in the word form consonants like "zh", "ch", etc.

The expected outcome of this Python program: Enter word to translate: else The word in Pig Latin is elseway. Enter word to translate: chip The word in Pig Latin is ipchay.

Upvotes: 0

Views: 1259

Answers (3)

Jason Richards
Jason Richards

Reputation: 21

This may not be the most pythonic answer, but...

When you use elif: you are adding to the first statement which is why it is always adding way to the end

Use elif to address each vowel then an else statement to handle the consonants:

vowels = ['a','e','i','o','u']
consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
word = input("Enter word to translate: ")
#if the 1st letter of a word is "aeiou", add "way" to the end of the word 
if word[0] in vowels:
    print(word + "way")

elif word[0] and word[1] in consonants:  
    print(word[2:] + word[0:2] + "ay") 

else:
    print(word[1:] + word[0] + 'ay')

Upvotes: 0

andole
andole

Reputation: 286

'or' does not work like you think it does. It sees if what's on the left is True, if it's not, then 'or' sees if what on the right is True. If any of those is True then 'or' returns True, otherwise it returns False. In your code a letter gets cast to a boolean, and that always gives True. So on the right from 'or' there is always True, which means that your first 'if' will always work.

Upvotes: 0

David Buck
David Buck

Reputation: 3844

The way Python is interpreting your if statements isn't the way you're thinking about it.

if word[0] == "a" or "e"

I think you're thinking of it as:

if word[0] == ("a" or "e")

but it is actually being processed as

if (word[0] == "a") or ("e"): 

so it doesn't matter what word[0] actually contains, as "e" (or any non-empty string) will always be evaluated as True by Python, so it will always add 'way'.

The easiest way to rewrite the if is

if word[0] in "aeiou":

That will actually check if it's a vowel, by checking if it appears in the string "aeiou"

Upvotes: 0

Related Questions