Knowee
Questions
Features
Study Tools

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

Question

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

🧐 Not the exact question you are looking for?Go ask a question

Solution

To count the number of vowels in the text file named "Poem.txt" using Python, you can follow these steps:

  1. Open the file using the open() function and assign it to a variable, let's say file.
  2. Read the contents of the file using the read() method and assign it to a variable, let's say content.
  3. Initialize a variable, let's say vowel_count, to keep track of the number of vowels.
  4. Create a list of vowels, such as vowels = ['a', 'e', 'i', 'o', 'u'].
  5. Iterate over each character in the content using a for loop.
  6. Check if the character is a vowel by using an if statement and the in operator to check if the character is in the vowels list.
  7. If the character is a vowel, increment the vowel_count variable by 1.
  8. After iterating through all the characters, display the vowel_count.

Here's an example implementation of the vowelCount() function:

def vowelCount():
    file = open("Poem.txt", "r")
    content = file.read()
    file.close()

    vowel_count = 0
    vowels = ['a', 'e', 'i', 'o', 'u']

    for char in content:
        if char.lower() in vowels:
            vowel_count += 1

    print("Number of vowels:", vowel_count)

You can call the vowelCount() function to count and display the number of vowels in the "Poem.txt" file.

This problem has been solved

Similar Questions

Write a short Python function num_vowels(text) that counts the number of vowels ina given character string

#Write a Python function count_vowels(text) that takes a string as input and returns the count of vowels (a, e, i, o, u) in the input string, ignoring case.

Write a function in python to read the content from a text file "poem.txt" line by line and display the same on screen. Solution

Write a python program to count the number of characters of alphabets 'O' and 'i' from the given file

Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.