Cannot determine vowels from consonants

前端 未结 5 1695
北恋
北恋 2021-01-07 00:28

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: \")
f         


        
5条回答
  •  遥遥无期
    2021-01-07 00:59

    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.

提交回复
热议问题