I'm creating a simple address book program in Python. Whenever I enter multiple contacts through the command line menu and then press "b" to list all the contacts, it only shows the last contact I entered. How can I make the program save all of the contacts?
# Import Collections to use Ordered Dictionary
import collections
# The main class
def main():
# Print the menu for program
print """
Contact Book App
a) New Contact
b) List Contacts
c) Search Contacts
d) Delete Contact
e) Quit
"""
# Creates an empty ordered dictionary
contact = collections.OrderedDict()
# Sets the loop as 1
loop = 1
# Create a while loop for the menu that keeps looping for user input until loop = 0
while loop == 1:
# Asks for users input from 1-5
userInput = raw_input("Please select an option: ").lower()
# OPTION 1 : ADD NEW CONTACT
if userInput == "a":
contact['name'] = raw_input("Enter name: ")
contact['phone'] = raw_input("Enter phone: ")
contact['email'] = raw_input("Enter email: ")
# Confirmation prompt
print "Contact Added!"
#For Debugging Purposes
# print(contact)
# OPTION 2 : LIST ALL CONTACTS
elif userInput == "b":
print "Listing Contacts"
print(contact)
# OPTION 3 : SEARCH CONTACTS
elif userInput == "c":
print "Searching Contacts"
print "Please Enter Contact Name"
# OPTION 4 : DELETE A CONTACT
elif userInput == "d":
print "Deleting Contact"
# OPTION 5 : QUIT PROGRAM
elif userInput == "e":
print "Quitting Contact Book"
loop = 0
else:
print "I did not understand your input"
main()
Here is my output:
Contact Book App
a) New Contact
b) List Contacts
c) Search Contacts
d) Delete Contact
e) Quit
Please select an option: a
Enter name: Dave Smith
Enter phone: 5553451212
Enter email: dsmith@gmail.com
Contact Added!
Please select an option: a
Enter name: John Doe
Enter phone: 4445433232
Enter email: jdoe@hotmail.com
Contact Added!
Please select an option: b
Listing Contacts
OrderedDict([('name', 'John Doe'), ('phone', '4445433232'), ('email', 'jdoe@hotmail.com')])
Please select an option:
As you can see only the last entry John Doe
appears and Dave Smith
has been overwritten.
Make a list and add new contacts to it:
contacts = []
...
# OPTION 1 : ADD NEW CONTACT
if userInput == "a":
contact = collections.OrderedDict()
contact['name'] = raw_input("Enter name: ")
contact['phone'] = raw_input("Enter phone: ")
contact['email'] = raw_input("Enter email: ")
contacts.append(contact)
...
contacts = []
# Sets the loop as 1
loop = 1
# Create a while loop for the menu that keeps looping for user input until loop = 0
while loop == 1:
# Asks for users input from 1-5
userInput = raw_input("Please select an option: ").lower()
# OPTION 1 : ADD NEW CONTACT
if userInput == "a":
contact = collections.OrderedDict()
contact['name'] = raw_input("Enter name: ")
contact['phone'] = raw_input("Enter phone: ")
contact['email'] = raw_input("Enter email: ")
contacts.append(contact)
# Confirmation prompt
print "Contact Added!"
#For Debugging Purposes
# print(contact)
# OPTION 2 : LIST ALL CONTACTS
elif userInput == "b":
print "Listing Contacts"
print(contacts)
[...]
Each time you ask the user for new contact details, you overwrite the previously created singular contact. Instead, you have to maintain a list of contacts, create a new contact and add that one to the list. Also, I'd suggest creating a class Contact
instead of just using a dictionary.
class Contact:
def __init__(self, name, phone, mail):
self.name = name
self.phone = phone
self.mail = mail
def __repr__(self):
return "Contact(%r, %r, %r)" % (self.name, self.phone, self.mail)
Also, you can use break
to exit the loop instead of using a variable. In your main
(excerpt):
...
contacts= [] # initialize list of contacts
while True:
userInput = raw_input("Please select an option: ").lower()
if userInput == "a":
name = raw_input("Enter name: ")
phone = raw_input("Enter phone: ")
email = raw_input("Enter email: ")
contacts.append(Contact(name, phone, email)) # create and add new contact
print "Contact Added!"
elif userInput == "b":
print "Listing Contacts"
print(contacts) # use contacts list here
... more options
elif userInput == "e":
print "Quitting Contact Book"
break # use break here
else:
print "I did not understand your input"
Every time that you create a new entry in your dictionary with the key 'name' you overwrite the previous one. It would be better to create a new list of dictionaries and store each person as a separate dictionary within that list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.