9
original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = str(word)[0]
    print first
    if str(first) == "a" or "e" or "i" or "u" or "o":
        print "vowel"
else:
    print "consonant"

I want to check if a word starts with a vowel or consonant. However, this part does not work: if str(first) == "a" or "e" or "i" or "u" or "o"

So how would you check if the first letter is either "a" or "e" or "i" or "u" or "o"?

tshepang
  • 11,360
  • 21
  • 88
  • 132
Alexander Nilsson
  • 111
  • 1
  • 1
  • 4

2 Answers2

15

Use in

if len(original) and original.isalpha():
    word = original.lower()
    first = word[0]
    print first
    if first in ('a','e','i','o','u'):
        print "vowel"
    else:
        print "consonant"

If you were trying to use OR clause you must use like this BUT it's not the better pythonic way:

if first =='a' or first =='e' or first =='i' or first =='o' or first =='u':
wjandrea
  • 23,210
  • 7
  • 49
  • 68
Fernando Freitas Alves
  • 3,571
  • 3
  • 24
  • 45
6
if str(first) == "a" or "e" or "i" or "u" or "o":

should moditied to

if str(first) in ("a", "e", "i", "o", "u"):

Python has a explicit demand on indent. Make sure you have a right indent.

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = str(word)[0]
    print first
    if str(first) in ("a", "e", "i", "o", "u"):
        print "vowel"
    else:
        print "consonant"
Yarkee
  • 8,494
  • 5
  • 27
  • 27