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 thenumber 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
contentusing a for loop. - Check if the character is a vowel by using an if statement and the
inoperator to check if the character is in thevowelslist. - If the character is a vowel, increment the
vowel_countvariable 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
Count the sentences, words and characters in a fileWrite a Python program that reads a file and prints the number of lines, words, and characters in the file.Input Format:The input will be a single string, which is the name of the file to be read.Output Format:The output will be three separate lines, each containing a count:The first line will contain the number of lines in the file, prefixed with "Sentences: ".The second line will contain the number of words in the file, prefixed with "Words: ".The third line will contain the number of characters in the file, prefixed with "Chars: ".Sample Test CasesTest Case 1:Expected Output:file·name:input1.txtSentences:·8Words:·34Chars:·228Test Case 2:Expected Output:file·name:input2.txtSentences:·7Words:·51Chars:·306
letter countWrite a C program that will analyze a line of text and will print the number of words, the number of consonants, and the number of vowels in the text.Testcase:Input:Programming is fun!Output:3 // Number of words:11 // Number of consonants:5 // Number of vowels:
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.