At Westonci.ca, we connect you with experts who provide detailed answers to your most pressing questions. Start exploring now! Discover comprehensive answers to your questions from knowledgeable professionals on our user-friendly platform. Get immediate and reliable solutions to your questions from a community of experienced professionals on our platform.

Write a function named count_vowels that accepts two arguments: a string and an empty dictionary. The function should count the number of times each vowel (the letters a, e, i, o, and u) appears in the string, and use the dictionary to store those counts. When the function ends, the dictionary should have exactly 5 elements. In each element, the key will be a vowel (lowercase) and the value will be the number of times the vowel appears in the string. For example, if the string argument is 'Now is the time', the function will store the following elements in the dictionary: 'a': 0 • 'e': 2 'i': 2 'o': 1 'u': 0 The function should not return a value.

Sagot :

The function that counts the number of times a vowel exist in a string is as follows:

def count_vowels(string, dictionary):

    vowels = 'aeiou'

    dictionary = {}.fromkeys(vowels, 0)

    for i in string:

         if i in vowels:

              dictionary[i] += 1

    return dictionary

print(count_vowels("trouble", {}))

Code explanation.

The code is written in python.

  • we defined a function named "count_vowels". The function accept a string and an empty dictionary.
  • Then. we store the vowels in a variable vowels.
  • The dictionary is used to store the key value pair i.e the vowels and the number of times they appear.
  • We looped through the string.
  • If any value in the string is in vowels,  we increase the dictionary values by 1.
  • Then, we return the dictionary.
  • Finally, we call the function with it parameters.

learn more on function here: https://brainly.com/question/27219031