Expand|Select|Wrap|Line Numbers
- #Created a dictionary
- word_dictionary = dict()
- #opening my filing and setting it in read mode and calling it f
- with open('findall.txt', 'r') as f:
- #Getting input from the user
- word = input("Enter the word to search for: ")
- #reading through the file
- content = f.read()
- #counts to see how many words there are
- total = content.count(word)
- #creating a new key value pair for word_dictionary
- word_dictionary['count'] = total
- #Trying to print the result
- print('The word count for ',word, 'is', word_dictionary[word]['count'])
Quote:
Enter the word to search for: life
Traceback (most recent call last):
File "main.py", line 13, in <module>
print('The word count for ',word, 'is', word_dictionary[word]['count'])
KeyError: 'life'