Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm able to count all of the words in my .txt file but I'm having a hard time putting that result inside of my dictionary. I tried adding a new key value pair to my dictionary, I don't know if I did it right. I called the key "count" and I am calling the value "total". I'm trying to grab the variable "total" and make it the value of my dictionary. I don't if you are allowed do that or not but I could use some help. I don't if it has anything to do with the error that I'm getting but the error message is right below my code. Here's my code:

Python
#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'])


I get an error when I run the program this is what it says:
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'


What I have tried:

I tried searching google for the key error that I'm getting but I wasn't able to find any results online.
Posted
Updated 7-Jun-21 21:25pm
v3

I'd suggest to start here: 5. Data Structures — Python 3.9.5 documentation[^].

Take a look at this small piece of code:
Python
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127


So, to add key-value-pair to your dictionaty, you should use:
Python
word_dictionary['your_word_here'] = count_of_word
   
Python
word_dictionary['count'] = total

You are using the key 'count' instead of the word chosen by the user. It should be:
Python
word_dictionary[word] = total
   
Comments
adam breslin 8-Jun-21 21:29pm
   
I just changed it up but now I'm getting another error that says:
TypeError: 'int' object is not subscriptable
Richard MacCutchan 9-Jun-21 3:49am
   
Yes, because word_dictionary[word] returns an int, and you are trying to apply the subscript [total] to it.
adam breslin 8-Jun-21 21:31pm
   
@Richard MacCutchan I changed the print statement to this, is this correct:
print('The word count for ', word, 'is', word_dictionary[word][total])
Richard MacCutchan 9-Jun-21 3:40am
   
Why are you trying to use a second index that does not exist? The valiue of the dictionary item is returned by the simple expression word_dictionary[word]. If you do not understand hoe to use dictionary types then go and study the information at the link that Maciej provided above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900