8

What silly mistake am I making here that is preventing me from determining that the first letter of user input is a consonant? No matter what I enter, it allows evaluates that the first letter is a vowel.

original = raw_input('Enter a word:')
word = original.lower()
first = word[0]

if len(original) > 0 and original.isalpha():
    if first == "a" or "e" or "i" or "o" or "u":
        print "vowel"
    else:
        print "consonant"
else:
    print "empty"
1
  • Another approach would be use to use the startswith() method. Assigning the word to the variable first you could then do the comparison: first == first.startswith(('a', 'e', 'i', 'o', 'u')); if True then you would know that this word does not begin with a consonant. Commented Jan 12, 2017 at 14:10

3 Answers 3

30

Change:

if first == "a" or "e" or "i" or "o" or "u":

to:

if first in ('a', 'e', 'i', 'o', 'u'):  #or `if first in 'aeiou'`

first == "a" or "e" or "i" or "o" or "u" is always True because it is evaluated as

(first == "a") or ("e") or ("i") or ("o") or ("u"), as an non-empty string is always True so this gets evaluated to True.

>>> bool('e')
True
Sign up to request clarification or add additional context in comments.

1 Comment

Or since this is all single letters, if first in 'aeiou':.
9

What you are doing in your if statement is checking if first == "a" is true and then if "e" is true, which it always is, so the if statement always evaluates to true.
What you should do instead is:

if first == "a" or first == "e" ...

or better yet:

if first in "aeiou":

Comments

4

Your issue is that first == "a" or "e" is being evaluated as (first == "a") or "e", so you're always going to get 'e', which is a True statement, causing "vowel" to be printed. An alternative is to do:

original = raw_input('Enter a word:')
word = original.lower()
first = word[0]

if len(original) > 0 and original.isalpha():
    if first in 'aeiou':
        print "vowel"
    else:
        print "consonant"
else:
    print "empty"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.