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
.
Solution
To count the number of vowels in the text file named "Poem.txt" using Python, you can follow these steps:
- Open the file using the
open()
function and assign it to a variable, let's sayfile
. - Read the contents of the file using the
read()
method and assign it to a variable, let's saycontent
. - Initialize a variable, let's say
vowel_count
, to keep track of the number of vowels. - Create a list of vowels, such as
vowels = ['a', 'e', 'i', 'o', 'u']
. - Iterate over each character in the
content
using a for loop. - Check if the character is a vowel by using an if statement and the
in
operator to check if the character is in thevowels
list. - If the character is a vowel, increment the
vowel_count
variable by 1. - 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.
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.
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.