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
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)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
