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

  • SEARCH
  • Home
  • 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 6813697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:36:02+00:00 2026-05-26T20:36:02+00:00

I have a file that I’m working with. I want to create a function

  • 0

I have a file that I’m working with. I want to create a function that reads the files and place the contents into a dictionary. Then that dictionary needs to be passed through the main function. Here is the main program. It cannot be changed. Everything I do must work with the main program.

def main():
    sunspot_dict = {}
    file_str = raw_input("Open what data file: ")
    keep_going = True
    while keep_going:
        try:
            init_dictionary(file_str, sunspot_dict)
        except IOError:
            print "Data file error, try again"
            file_str = raw_input("Open what data file: ")    
            continue
        print "Jan, 1900-1905:", avg_sunspot(sunspot_dict, (1900,1905),(1,1))
        print "Jan-June, 2000-2011:", avg_sunspot(sunspot_dict, (2000,2011), (1,6))
        print "All years, Jan:", avg_sunspot(sunspot_dict, month_tuple=(1,1))
        print "All months, 1900-1905:", avg_sunspot(sunspot_dict, year_tuple=(1900,1905))
        try:
            print "Bad Year Key example:", avg_sunspot(sunspot_dict, (100,1000), (1,1))
        except KeyError:
            print "Bad Key raised"
        try:
            print "Bad Month Index example:", avg_sunspot(sunspot_dict, (2000,2011), (1,100))
        except IndexError:
            print "Bad Range raised"
        keep_going = False
    print "Main function finished normally."

    print sunspot_dict

Here is what I have so far:

def init_dictionary(file_str, sunspot_dict):
    line = open(file_str, "rU")
    sunspot_dict = {}
    for i in line:
        sunspot_dict[i]
print sunspot_dict
  • 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-05-26T20:36:03+00:00Added an answer on May 26, 2026 at 8:36 pm

    Based on your second comment and your preview code, you are on the right track. Dictionaries in Python are passed by reference, so you should just be able to fill the dictionary provided to init_dictionary and the values will be accessible in main(). In your case you are creating a new dictionary in init_dictionary with the line sunspot_dict = {}. You don’t want to do this, because the dictionary you want to use was already created in main().

    At this point we would need to know the format of the file you are reading. Basically you need to open the file, then probably read it line by line while parsing the lines into key/value pairs and filling sunspot_dict.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that I've been working on and then realized that I
I have a file that I want to include in Python but the included
I have a file that I want to read in using the File::Slurp module
I have a file that describes input data, which is split into several other
I have a file that I read into a char array. The char array
I have a file that reads like this 2012-021 24X7 Online Tutoring Company Applied
I have a file that keeps appending the following xml. What I want to
I have a file that I only want to extract cells B9, B19, B29,
I have a file that <a href=blah.com rel=blahblah style=textdecoration:none;>blah</a> I want to match rel=blahblah
I have a file that I want to read and write to a binary

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.