[Tutor] Differentiating vowels from consonants

Alan Gauld alan.gauld at btinternet.com
Sun Oct 26 14:31:23 CET 2014


On 26/10/14 09:01, William Becerra wrote:

> word = raw_input("Who do You Support: ")
> vowels = ('a', 'e', 'i', 'o', 'u')

You could just use a string

vowels = 'aeiou'

> for letter in word:
>      if letter == vowels:

you want

if letter in vowels:

>          call = 'Give me an ' + letter + '!'
>          print call
>          print letter + '!'
>      else :
>          call = 'Give me a ' + letter + '!'
>          print call

But I'd use a helper function instead:

def article(letter):
     return 'an' if letter in 'aeiou' else 'a'

and write the call assignment as

call = 'Give me ' + article(letter) + letter + '!'

Which saves the if/else structure with its repeated code.
Of course you may not have covered functions yet in which
case your solution is fine.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list