Cannot determine vowels from consonants

2020-03-07 06:53发布

With the code below, no matter what the first letter of the input is, it is always determined as a vowel:

original = raw_input("Please type in a word: ")
firstLetter = original[0]
print firstLetter

if firstLetter == "a" or "e" or "i" or "o" or "u":
    print "vowel"
else:
    print "consonant"

In fact, it doesn't matter what the boolean is in the if statement... if it is == or != , it is still return "vowel". Why?

5条回答
三岁会撩人
2楼-- · 2020-03-07 07:04

Your test doesn't work like you think it does. You are asking, "If the variable equals 'a'", then the rest of your test basically asks if the letters e, i, o or u are False. Only an empty string evaluates to False (try "e" == False in the interpreter to see). Use the "in" test from other answers. Also, make sure to lower-case firstLetter so you're always comparing like to like.

查看更多
冷血范
3楼-- · 2020-03-07 07:04

First you should know how or works, it evaluates both the left and right expression and it returns the first expression that evaluates true, otherwise it returns the last expression:

From this source you can see that all of these evaluate false:

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a nonzero() or len() method, when that method returns the integer zero or bool value False.3.1

>>> False or True
True
>>> '' or False
False
>>> '' or 0
0

So a non-empty string will evaluate as true and it will be returned:

>>> 'abc' or False
'abc

 if firstLetter == "a" or "e" or "i" or "o" or "u":
     print "vowel"
 else:
     print "consonant"

For a string 'foo', 'f' is the first letter. In the first part of your code, firstLetter == "a" or "e", the left expression will evaluate as false, but 'e' is not an empty string so it evaluates as true and it will print "vowel"

You can see my other answer here which answers your question, something like this:

if c.upper() in "AEIOU"

will check if a letter is a vowel.

查看更多
爷、活的狠高调
4楼-- · 2020-03-07 07:06
if firstLetter in ("a", "e", "i", "o", "u"):

what your test does is the following:

if (firstLetter == "a") or "e" or "i" or "o" or "u":

and each of the latter 4 tests is true.

查看更多
乱世女痞
5楼-- · 2020-03-07 07:14

Python is not the English language. If you have a bunch of expressions with or or and between them, each one must make sense on its own. Note that on its own:

if "e":
    print("something")

will always print something, even if letter doesn't equal "e".

You need to do it like this:

if letter == "a" or letter == "e"  # (...)

Or, more concisely:

if letter in "aeiouy":
查看更多
我想做一个坏孩纸
6楼-- · 2020-03-07 07:15

Try this for your Boolean expression:

firstLetter = 'a'
firstLetter in 'aeiou' 
True

firstLetter = 'x'
firstLetter in 'aeiou' 
False

This is equivalent to

      firstLetter in ['a', 'e', 'i', 'o', 'u']

i.e., you want to put this into your if statement like this:

if firstLetter  in 'aeiou':
   print 'vowel'
else:
   print 'consonant'

Note:

Your original approach was on the right track, but you would have had to compare each letter separetely like

if firstLetter == 'a' or firstLetter == 'e' or firstLetter == 'i' or ... 

Using the above is much more concise.

查看更多
登录 后发表回答