Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Write a function, vowelCount() in Python that counts and displays the number of vowels in the text file named Poem.txt.

Answer

Answer by student:

Function in Python that counts and displays the number of vowels in the text file - Teachoo.png

Detailed answer by teachoo:

  • The question asks us to write a function in Python that counts and displays the number of vowels in the text file named Poem.txt.
  • To do this, we need to use the following steps:
    • Define the function using the def keyword, followed by the function name and parentheses. In this case, we name the function vowelCount().
    • Open the file using the open() function, which takes the file name and the mode as arguments. The mode can be “r” for reading, “w” for writing, “a” for appending, or “r+” for both reading and writing. In this case, we use “r” mode to read the file.
    • Create a string variable named vowels that contains all the lowercase and uppercase vowels. We will use this variable to check if a character is a vowel or not.
    • Create an integer variable named count that will store the number of vowels in the file. We initialize it to zero.
    • Loop through each line in the file using a for loop. The open() function returns a file object that can be iterated over using a for loop.
    • Loop through each character in the line using another for loop. We can access each character in a string using indexing or slicing.
    • Check if the character is in the vowels string using the in operator, which returns True if an element is present in a sequence, or False otherwise. For example, “a” in “aeiou” returns True, but “b” in “aeiou” returns False.
    • If the character is a vowel, increment the count variable by one using the += operator, which adds a value to a variable and assigns it back to the same variable. For example, count += 1 is equivalent to count = count + 1.
    • Print the value of the count variable using the print() function, which displays the value of its argument to the standard output. We can use string concatenation (+) or formatting (% or .format()) to combine strings and variables in a print statement. For example, print(“The number of vowels is” + str(count)) or print(“The number of vowels is %d” % count) or print(“The number of vowels is {}”.format(count)) will all print the same output.
    • Close the file using the close() method, which frees up any system resources used by the file object. It is a good practice to close any files that we open after we are done with them.

So, the correct answer is:

Function in Python (with comments) that counts and displays the number of vowels in the text file - Teachoo.png

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.