-1

I am new to programming, and am learning on Python 3.4. I am working on an exercise to determine if a user defined letter is a vowel or consonant.

My original if statement looked as follows:

letter = input('Enter a letter')

if letter == 'a' or 'e' or 'i' or 'u':
    print('your letter is a vowel')
else:
print ('your letter is a consonant')

this returned that my letter was a vowel every time.

I have since learned the answer was to have an if statement that looks as follows:

if letter == 'a' or letter == 'i' or .......

My question is why did my original if statement return that it was a vowel every time, and not give me an error?

My though process is that it is verifying that it is a string.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
DntMesArnd
  • 115
  • 9

1 Answers1

1

Any non-zeroish value evaluates to True in a condition. Also, comparison operators (== in this case) have higher priority than logical operators (or,and,not). (Details: https://docs.python.org/3/library/stdtypes.html)
So python interpreted your if statement as:

if (letter == 'a') or ('e') or ('i') or ('u'):

Since 'e' is not zero or zero-like, that part of the if was true, so your expression was true.

An easier approach than your working solution is:

if letter in 'aeiou':

Which checks if letter is a character in the string.

happydave
  • 7,127
  • 1
  • 26
  • 25