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

PYTHON : A useful application for a dictionary is to remember, or cache, previou

ID: 3691273 • Letter: P

Question

PYTHON : A useful application for a dictionary is to remember, or cache, previously obtained results so that they can be retrieved from the cache when they are a new. Modify the program in problem #3 above so that the user can repeatedly enter filenames. If the user enters the same filename more than once, look up the answer from a dictionary instead of counting the words again.

# (Problem 3 )Write a program that counts how often each word occurs in a text file. Enter a file name, put the words (using split) into a
# dictionary, then prints each word and how many times it was used.

# this is what i used for problem number 3
userInput = input('Enter the file name: ')

fileOpen = open(userInput, "r")

counter = dict()
for line in fileOpen:
words = line.split()# splits words in files
for word in words: # create counter for each time word appears
if word not in counter:
counter[word] = 1
else:
counter[word] += 1

print (counter)

Explanation / Answer

ANS;

you just need to change the main for this.

new main will be

def main():

for filename in sys.argv[1:]:
words = count_words(filename)
print_dictionary(words)