Need help with this problem, I keep getting stuck and might be starting it wrong
ID: 3882769 • Letter: N
Question
Need help with this problem, I keep getting stuck and might be starting it wrong.
In Python 2, Write a program that manages the email address of our contacts. Your program must contain a class named contact that has 3 attributes (instance variables): last name, first name, and email. Your class should also have a method to return the name and email address for printing when requested.
Each contact should use a single instance of your contact class. You should use either a list or a dictionary to act as a container of the contact instances.
The program should have a menu that allows the user to interact with your collection of contacts: displaying contacts or adding new contacts as long as the user wishes, then saying goodbye when finished. The menu should offer these options.
Program Options.
1.) Display all contacts
2.) Create new contact
3.) Exit
option = raw_input("Enter 1, 2, or 3: ")
Save and retrieve the dictionary or list of instances to/from disk using the pickle module.
Example Output:
Program Options 1.) Display all contacts 2.) Create new contact 3. Save and exit Enter 1, 2, or 3: 1 Name smith, sue Dean, hugh green, am Email ssmith@wer.com hrdeangabc.com sgreen@rty.com Program Options 1.) Display all contacts 2.) Create new contact 3. Save and exit Enter 1, 2, or 3: 2 Enter contact's first name: mac Enter contact's last name davis Enter contact email: mdavis@asd.corm Program Options 1.) Display all contacts 2.) Create new contact 3. Save and exit Enter 1, 2, or 3: 1 Name smith, sue Dean, hugh green, am davis, mac Email ssmith@wer.com hrdeangabc.com sgreen@rty.com mdavis Casd.com Program Options 1.) Display all contacts 2.) Create new contact 3. Save and exit Enter 1, 2, or 3:Explanation / Answer
#!usr/bin/python
class Contact:
def __init__(self, fname, lname, email):
self.fname = fname
self.lname = lname
self.email = email
def getFirstname(self):
return fname
def getLastname(self):
return lname
def getEmail(self):
return email
option = 0
file1 = open("Contacts.txt", 'r') #Assuming contacts are in this file.Format is every line conatins first name, last name and email sepaarted by space,
List = []
for line in file1:
List.append(Contact(line.split()[0], line.split()[1], line.split()[2]))
file1.close()
while option!=3:
print("Program Options.")
print(" 1.)Display all contacts")
print(" 2.)Create new contact")
print(" 3.)Save and Exit");
option = int(input("Enter 1,2, or 3 :"))
if option == 1:
if len(List) == 0:
print("List is empty")
else:
print("Name Email")
for i in List:
print(i.getFirstname(),i.getLastname()+" " , i.getEmail())
if option == 2:
fname = input("Enter contact's first name:")
lname = input("Enter contact's last name:")
email = input("Enter contact's email:")
List.append(Contact(fname, lname,email))
if option == 3:
file2 = open("Contacts.txt", 'w')
for i in List:
file2.write(i.getFirstname() + ' ' + i.getLastname() + ' ' + i.getEmail() +' ')
file2.close()
print("GoodBye..")