r/learnpython • • Jun 23 '13

Anybody know why my code will not read the second If Statement?

I was making a code as part of the CodeAcademy's learning Python thing and it wants to make a Pig Latin Translator.

The code works fine up until I want to check to see if the first letter is a vowel on Line 6. The code just prints "vowel", regardless of whether the word I type in is "angus" or "piggy". I've switched the "==" to a "!=" just to see if it would change anything; it didnt. This makes me think that the code isn't reading the If Statement?

I know that word[0] is working and its accurate, I just dont know if my syntax is wrong/stupid. Any help would be appreciated. (Also its unfinished, just so you know).

pyg = 'ay'
original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    if word[0] == "a" or "e" or "i" or "o" or "u":
        print "vowel"
    else:
        print "consonant"
else:
    print 'empty'
6 Upvotes

6 comments sorted by

12

u/K900_ SRE person // please don't DM me your questions Jun 23 '13

if word[0] == "a" or "e" or "i" or "o" or "u":

You think it is if word[0] == ("a" or "e" or "i" or "o" or "u"):, but it's not. It's if (word[0] == "a") or "e" or "i" or "o" or "u":. "e" is a non-empty string, so it's truthy. So the whole condition is truthy. To check if an object is in a sequence, use in, like this: if word[0] in "aeoiu":

1

u/tired_papasmurf Jun 23 '13

That makes so much more sense, thanks.

I'll use in , but could I alternatively use the parenthesis like in your first sentence?

3

u/zahlman zahlman Jun 24 '13

No; see my other reply.

To make or work, you would need each thing on each side of the or to be one of the things you actually want to consider.

In your case, there are five possibilities you want to consider:

word[0] == 'a'
word[0] == 'e'
word[0] == 'i'
word[0] == 'o'
word[0] == 'u'

So now you can figure out what you'd need to write, and should also have a pretty good idea of why in is strongly preferred.

5

u/AYNRAND420 Jun 23 '13

yep, so you want:

if word[0] == "a" or word[0] == "e" or word[0] == "i" or word[0] == "o" or word[0] == "u":

but more simply as k900_ stated:

if word[0] in "aeiou":

meaning if the word appears in that string at all, the condition will be true.

3

u/[deleted] Jun 23 '13

And just to add a little side note on the topic of "and/or": The and-operator will return the LAST true value if all values are true, but if any of the values are false, it'll return the first false value.

The or-operator is basically the opposite of and (kind of). If there are any true values, it'll return the first true value, unless all the values are false, then it will return the last false value. It essentially looks for the 1st true value, returns it & ignores the rest.

While it isn't specifically relevant to your problem, it may help you sort out your thoughts when dealing with future logic problems.

3

u/zahlman zahlman Jun 24 '13

You think it is if word[0] == ("a" or "e" or "i" or "o" or "u"):, but it's not

Even if it were, that wouldn't help: ("a" or "e" or "i" or "o" or "u") is not some magical thing that you can compare word[0] to in order to find out of word[0] is any of those things. ("a" or "e" or "i" or "o" or "u") is just like any other use of or: is the left-hand side true? If it is, we'll use it, otherwise we'll use the right-hand side. "a" is true (it's a non-empty string), so ("a" or "e" or "i" or "o" or "u") is just "a".