Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Vowels vs Consonants In Python [duplicate]

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"
like image 936
THE DOCTOR Avatar asked Nov 26 '13 19:11

THE DOCTOR


People also ask

How do you check if a letter is a vowel or consonant in Python?

Method 1: Users can use built-in functions to check whether an alphabet is vowel function in python or not. Step 2: Using built-in python functions like (lower(), upper()), determine whether the input is vowel or consonant. Step 3: If the character is a vowel, it should be printed.

How do you identify a vowel and consonant sound?

A vowel is a speech sound made with your mouth fairly open, the nucleus of a spoken syllable. A consonant is a sound made with your mouth fairly closed.

How do you check if a string is a vowel or not in Python?

Approach : Firstly, create set of vowels using set() function. Check for each character of the string is vowel or not, if vowel then add into the set s. After coming out of the loop, check length of the set s, if length of set s is equal to the length of the vowels set then string is accepted otherwise not.


1 Answers

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
like image 102
Ashwini Chaudhary Avatar answered Sep 22 '22 00:09

Ashwini Chaudhary