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 7036763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:27:47+00:00 2026-05-28T01:27:47+00:00

I’m trying to write a program that creates an address book with contact names,

  • 0

I’m trying to write a program that creates an address book with contact names, emails, phone numbers, etc. I store each contact as a dictionary and then place each person (dictionary) into a global list. I then convert the list to a string using repr() and write it to a file. When I try to reload the list and write what it contains, I get a list of empty dictionaries. Please help me figure out what is wrong.

Here is my code:

list = []
listfile = 'phonebook.txt'

class bookEntry(dict):
    total = 0

    def __init__(self):
        bookEntry.total += 1
        self.d = {}

    def __del__(self):
        bookEntry.total -= 1

class Person(bookEntry):
    def __init__(self, n):
        bookEntry.__init__(self)
        self.n = n
        print '%s has been created' % (self.n)

    def addnewperson(self, n, e = '', ph = '', note = ''):
        f = file(listfile, 'w')

        self.d['name'] = n
        self.d['email'] = e
        self.d['phone'] = ph
        self.d['note'] = note

        list.append(self)
        listStr = repr(list)
        f.write(listStr)

        f.close()

I start the program with a startup() function:

def startup():
    aor = raw_input('Hello! Would you like to add an entry or retrieve one?')
    if aor == 'add':
        info = raw_input('Would you like to add a person or a company?')
        if info == 'person':
            n = raw_input('Please enter this persons name:')
            e = raw_input('Please enter this persons email address:')
            ph = raw_input('Please enter this persons phone number:')
            note = raw_input('Please add any notes if applicable:')

            X = Person(n)
            X.addnewperson(n, e, ph, note)
startup()

I add these answers to the prompts:

'''
    Hello! Would you like to add an entry or retrieve one?add
    Would you like to add a person or a company?person
    Please enter this persons name:Pig
    Please enter this persons email address:pig@brickhouse.com
    Please enter this persons phone number:333-333-3333
    Please add any notes if applicable:one of three
    Pig has been created
'''

When I open phonebook.txt, this is what I see:

[{}]

Why are empty dictionaries being returned?

  • 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-28T01:27:47+00:00Added an answer on May 28, 2026 at 1:27 am

    You’re deriving from dict, but storing all the elements in a member d. Hence, repr gives you a string representing an empty dict. If you want to use a bookEntry as a dict, insert the info with

    self['name'] = n
    

    instead of

    self.d['name'] = n
    

    (But really, you shouldn’t be inheriting from dict here. Also, please don’t use list as an identifier, it’s the name of a builtin.)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.