Question:
Define a function isVowel(char) that returns True if char is a vowel (‘a’, ‘e’, ‘i’, ‘o’, or ‘u’), and False otherwise. You can assume that char is a single letter of any case (ie, ‘A’ and ‘a’ are both valid).
Do not use the keyword in. Your function should take in a single string and return a boolean.
Code Given:
def isVowel(char):
'''
char: a single letter of any case
returns: True if char is a vowel and False otherwise.
'''
My Code:
def isVowel(char):
'''
char: a single letter of any case
returns: True if char is a vowel and False otherwise.
'''
if char == 'a' or 'e' or 'i' or 'o' or 'u' or 'A' or 'E' or 'I' or 'O' or 'U':
return True
else:
return False
My Problem:
My output is always True. What am I doing wrong?
Your if statement:
is equivalent to:
which will always be evaluated to either
True, orewhich is alsoTrue, and hence your function always returnsTrue.Change your if-statement to:
But, this problem is really simple if you can use
inoperator. This goes like this: