Is there a way I can use an if function to determine if the first letter of a string is a vowel and if not, then print something else?

I've done quite a bit of research and the answers I found were too far advanced for me to understand. This is a small challenge I've handed myself that is based off of CodeAcademy's 'Pyg Latin' Translator

translate = input("Translate: ")
cons = "ay"
vowel = "way"
if len(translate) > 0:
    switch = str(translate[0])
    if str(translate[0]) != "a, e, i, o, u":
        new_word = str(translate) + str(switch) + str(cons)
        print (new_word.lower())

    new_word = str(translate) + str(vowel)
    print (new_word.lower())

    print("Please type something")

The job is to add "Way" or "Ay" depending on whether 'translate' begins with a vowel. I feel as though i'm skipping over something completely obvious but I can't figure it out. The program is pretty much ignoring line 6, Jordan comes out as Jordanjay Andrew comes out as Andrewaay

1 Answers

ncica On

in line :

if str(translate[0]) != "a, e, i, o, u":

you are taking first letter of translate (translate[0])

for example you have translate = 'example', the first letter is 'e'

you are comparing(!= which mean DIFFEREN) 'e' with another string "a, e, i, o, u", your first letter will alvays be different than string "a, e, i, o, u", you need to check if your first leter is not in that string, you can use:

if str(translate[0]) not in "a, e, i, o, u":

or you can check if your letter is not in the list:

if str(translate[0]) not in ['a', 'e', 'i', 'o', 'u']:

How do I check if something is (not) in a list in Python? The cheapest and most readable solution is using the in operator (or in your specific case, not in). As mentioned in the documentation,

The operators in and not in test for membership. x in s evaluates to True if x is a member of s, and False otherwise. x not in s returns the negation of x in s.


The operator not in is defined to have the inverse true value of in.

y not in x is logically the same as not y in x.

Here are a few examples:

'a' in [1, 2, 3]
# False

'c' in ['a', 'b', 'c']
# True

'a' not in [1, 2, 3]
# True

'c' not in ['a', 'b', 'c']
# False