python - Function that takes a string as input and counts the number of times the vowel occurs in the string -
so new python , in process of learning basics. trying create function counts number of vowels in string , returns how many times each vowel occurs in string. example if gave input, print out.
>>>countvowels('le tour de france') a, e, i, o, , u appear, respectively, 1,3,0,1,1 times.
i made helper function use, i'm not sure how use it.
def find_vowels(sentence): count = 0 vowels = "aeiuoaeiou" letter in sentence: if letter in vowels: count += 1 print count
and thought maybe use formatting them in write places, not sure notation used example, 1 of lines function be:
'a, , i, o, , u appear, respectively, {(count1)}, {(count2)}, {(count3)}, {(count4)}, {(count5)} times'
i not sure how able fit above in function.
you'd need use dictionary store values, since if directly add counts lose information vowel counting.
def countvowels(s): s = s.lower() #so don't have worry upper , lower cases vowels = 'aeiou' return {vowel:s.count(vowel) vowel in vowels} #a bit inefficient, easy understand
an alternate method be:
def countvowels(s): s = s.lower() vowels = {'a':0,'e':0,'i':0,'o':0,'u':0} char in s: if char in vowels: vowels[char]+=1 return vowels
to print this, this:
def printresults(result_dict): print "a, e, i, o, u, appear, respectively, {a},{e},{i},{o},{u} times".format(**result_dict)
Comments
Post a Comment