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

[Sample Paper Class 12] Write a function countVowels(WORDS) in Python, - Teachoo Sample Paper - Computer Science Class 12

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:

part 2 - Question 21 (Choice 1) - Teachoo Sample Paper - Computer Science Class 12 - Solutions to CBSE Sample Paper - Computer Science Class 12 - Computer Science - Class 12

-endlock-

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.