473,287 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Why am I getting a key error when I print my dictionary?

1 Bit
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:


Expand|Select|Wrap|Line Numbers
  1. #Created a dictionary
  2. word_dictionary = dict()
  3.  
  4. #opening my filing and setting it in read mode and calling it f
  5. with open('findall.txt', 'r') as f:
  6.   #Getting input from the user
  7.   word = input("Enter the word to search for: ")
  8.   #reading through the file
  9.   content = f.read()
  10.   #counts to see how many words there are
  11.   total = content.count(word)
  12.   #creating a new key value pair for word_dictionary
  13.   word_dictionary['count'] = total
  14.  
  15. #Trying to print the result
  16. print('The word count for ',word, 'is', word_dictionary[word]['count'])
  17.  
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'
Jun 8 '21 #1
1 2697
Banfa
9,065 Expert Mod 8TB
The problem is in
Expand|Select|Wrap|Line Numbers
  1. word_dictionary[word]['count']
The variable word contains the value 'life' but you have never created a key 'life' you only created a key 'count'. Additionally you are using double [] on a type that only really uses single [] for data access.

Did you mean to write
Expand|Select|Wrap|Line Numbers
  1. word_dictionary['count']
?

However I also note that the use of a dictionary in this code is unnecessary, you could equally well just use the variable total at line 16. Unless you are planning to alter the code to count the occurrence of multiple or all words. However in that case you would be better off using the word you are counting as the key not 'count' so that your dictionary can store multiple values.
Jun 8 '21 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Imran | last post by:
I m getting this error when i open asp.net Form Design View. "The file failed to load in the Web Form Designer. PLease Correct the following error, then load it again. The designer could not be...
1
by: dipesh | last post by:
I am getting error when trying to Add Web Reference in Visual Studio 2005. "Some of the files required for web references are not installed"
1
by: siddu | last post by:
Hi All, I have to do one task. First i need to copy files from vss and run bat files.i have a document with all steps. step 1: we have to take files from vss by using getlatestversion...
1
by: PulkitZery | last post by:
Hi, I am getting the following error when calling a method which i defined in web service. Any Idea?...
2
by: mak1084 | last post by:
i friends i'm getting this error when i execute the insert query.... what is the problem and how i can execute the my query? :?
1
by: jonny | last post by:
Went from using Visual Web Develop express to Visual Studio 2005 and getting error when trying to open project. Error message: "One or more projects in the solution could not be loaded for the...
0
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I have WCF service and integrated with STS (WS-Federation). The Service exposes 5 endpoints Win, UNT and Federated Windows, Federated UNT and Mex endpoint. My STS Service exposes 3 endpoints...
4
by: sumit kale | last post by:
Hi, Can somebody help me resolve my problem ? I am getting error when calculating total using unbound textfiled in subform. I have a main form called purchase_register_master and a subform...
1
by: NareshN | last post by:
Hi, I am getting error in following stored procedure in where condition Must declare the scalar variable "@CampaignId".I already declared CampaignId but still getting error.Please see where...
1
by: pallabtrue | last post by:
hello bytes friends, I am trying to run simple perl script with linux command where I am using 'awk' command and my perl script is ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.