'A' or 'An'? How to do multiple checks on vowels?


#1



https://www.codecademy.com/courses/python-beginner-en-cxMGf/2/5?curriculum_id=4f89dab3d788890003000096


I want to change 'A' by 'An' if needed. I managed to do so, but I think there is a shorter way.


Replace this line with your code. 
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']

print 'You have...'
for f in fruits:
    if f == 'tomato':
        print 'A tomato is not a fruit!' # (It actually is.)
        continue
    if f[0]=='a' or f[0]=='e' or f[0]=='i' or f[0]=='o' or f[0]=='u':
        print 'An', f
    else:
        print 'A', f
else:
    print 'A fine selection of fruits!'


#2

if f[0] in ['a', 'e', 'i', 'o', 'u']:

# or ...

vowels = ['a', 'e', 'i', 'o', 'u']

if f[0] in vowels:

#or...

vowels = ('a', 'e', 'i', 'o', 'u')

if f.startswith(vowels):

self edit:
that last one uses () instead of [] because it's a tuple.
A tuple is similar to an array, except its values can't be changed.
With a constant list (e.g., vowels) it's nice to have something like that.


#3

Thanks, that solved the issue. I liked the intro of a 'tuple' (new to me)


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.