Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8707277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:49:49+00:00 2026-06-13T03:49:49+00:00

I am attempting to run a python program that can run a dictionary from

  • 0

I am attempting to run a python program that can run a dictionary from a file with a list of words with each word given a score and standard deviation. My program looks like this:

theFile = open('word-happiness.csv', 'r')

theFile.close()



def make_happiness_table(filename):
   ''' make_happiness_table: string -> dict
       creates a dictionary of happiness scores from the given file '''

return {}


make_happiness_table("word-happiness.csv")

table = make_happiness_table("word-happiness.csv")
(score, stddev) = table['hunger']
print("the score for 'hunger' is %f" % score)

I have the word ‘hunger’ in my file but when I run this program to take ‘hunger’ and return its given score and std deviation, I get:

(score, stddev) = table['hunger']
KeyError: 'hunger'

How is it that I get a key error even though ‘hunger’ is in the dictionary?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:49:51+00:00Added an answer on June 13, 2026 at 3:49 am

    "hunger" isn’t in the dictionary (that’s what the KeyError tells you). The problem is probably your make_happiness_table function. I don’t know if you’ve posted the full code or not, but it doesn’t really matter. At the end of the function, you return an empty dictionary ({}) regardless of what else went on inside the function.

    You probably want to open your file inside that function, create the dictionary and return it. For example, if your csv file is just 2 columns (separated by a comma), you could do:

    def make_happiness_table(filename):
        with open(filename) as f:
             d = dict( line.split(',') for line in f )
             #Alternative if you find it more easy to understand
             #d = {}
             #for line in f:
             #    key,value = line.split(',')
             #    d[key] = value
        return d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to run a python program that can run a dictionary from
I am attempting to run some unit tests in python from what I believe
I'm attempting to run a file that calls multiple files, but I'm getting some
I am attempting to install libxml2 so that I can setup the python bindings
I'm attempting to run the soaplib Hello World program: import soaplib from soaplib.core.service import
I am attempting to run a number of student FORTRAN programs from a python
From the Django shell (manage.py shell), when attempting to import a python module that
I am attempting to wrap some C code in Python using Pyrex. I've run
When attempting to run the following assembly program: .globl start start: pushq $0x0 movq
I am attempting to run a command as root from a php page, is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.