Write a function countVowels(WORDS) in Python, that takes the dictionary, WORDS as an argument and displays the words (in lowercase) that have more than 2 vowels in them.
For example, Consider the following dictionary
WORDS={1:"Apple",2:"Orange",3:"Banana",4:"Grape",5:"Kiwi"}
The output should be:
apple
orange
banana
grape
Answer:
Answer by student
Detailed answer by teachoo
-lock-
-
The function countVowels(WORDS) in Python takes a dictionary, WORDS as an argument and displays the words (in lowercase) that have more than 2 vowels in them. To write this function, we need to follow these steps:
- Define a string variable vowels that contains all the lowercase vowels: “aeiou”.
- Loop through the values of the dictionary WORDS using a for loop . For each value, assign it to a variable word and convert it to lowercase using the lower() method.
- Initialize a variable count to zero. This variable will keep track of the number of vowels in the word.
- Loop through the letters of the word using another for loop . For each letter, check if it is in the vowels string using the in operator. If yes, increment the count by one.
- After the inner loop, check if the count is greater than 2 using an if statement. If yes, print the word using the print() function.
- The code for this function is:
-endlock-