Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Question: Write a function named initialVowels that analyzes the contents

ID: 3680707 • Letter: P

Question

Python Question:

Write a function named initialVowels that analyzes the contents of a text file for words that begin with a vowel. The letters a, e, i, o and u are vowels. The function initialV|owels should return a dictionary of vowel:word-list pairs. Each vowel should be a key in the dictionary if and only if it is the first letter of some word in the input file. The value of a key is a list of all the words in the input file in which that vowel is the first letter. Input. The function initialVowels takes a single parameter: i. inFile, a suing that is the name of a text file. This file contains only upper and lower case letters and white space (no punctuation marks or other special characters). It is in the current working directory. Output. Return a vowel:word-list dictionary For example, if the file named catInTheHat.txt contains the same text as in Question 12, then the function call print(initialVowels('catInTheHat.txt')) should output {'i': [' it', 'in', 'i', 'i', 'i'], 'a': ['all', 'and']}

Explanation / Answer

def initialVowels(filename):
list = [] #empty list
with open(filename,'r') as f: # open file
for line in f:
for word in line.split():   
list.append(word) # put words in list

for word in list:
word.lower() # convert words in list to lower case

myDict = {} # empty dictionary

for i in range(len(list)):
words = list[i].split()
for word in words: # checking each word
if word[0] in 'aeiou':
if word[0] not in myDict: # if starting of word is aeiou
myDict[word[0]] = [word] # put word in dictionary
else:
myDict[word[0]].append(word)

return myDict # print dictionary


print(initialVowels("filename"))