john
john

Reputation: 43

function vowel_count that take a string as inputs count the number of occurrence and prints the occurrence

I need to print the number of vowel occurrences in the string. I am able to count and print them in one line but I am having issue to print 'a,e,i,o and u' respectively on occurrence. I am not allowed to use any built in function. Can some one please guide or let me know what I am missing. Below is my code.

vowels = 'aeiou'
def vowel_count(txt):
    for vowel in vowels:
        print (txt.count(vowel),end ='')
    return

It will print the occurrence but I am not able to add anything in front of it. Lets say I pass le tour de france it should print a,e,i,o and u appear , respectively ,1,3,0,1,1 times.

Please let me know if any thing is unclear, thanks.

Upvotes: 1

Views: 897

Answers (2)

smac89
smac89

Reputation: 43186

Using list comprehension, the following can be achieved:

vowels = 'aeiou'
def vowel_count(txt):
    counts = map(txt.count, vowels)
    return ", ".join(vowels) + " appear, respectively, " + ",".join(map(str, counts)) + " times"

Upvotes: 0

AChampion
AChampion

Reputation: 30268

Just print before and after the loop your top and tail text:

def vowel_count(txt):
    print('a,e,i,o and u appear , respectively ', end='')
    for vowel in vowels:
        print(',', txt.count(vowel), sep='', end='')
    print(' times')

>>> vowel_count('le tour de france')
a,e,i,o and u appear , respectively ,1,3,0,1,1 times

But isn't print a built in function? I'm not sure how you can complete this task without using any built in functions.

Upvotes: 2

Related Questions