(3) Add a new contact record to the phone book. e marks] If the second option of
ID: 3746813 • Letter: #
Question
(3) Add a new contact record to the phone book. e marks] If the second option of the menu ie, 'Add a new contact, is selected, the program should allow the user to add a new contact record to the phone book. Its exact layout should have the same format as shown below. -- A Phone Book Management Program by abcd001 -- 1. Look up a contact 2. Add a new contact COMPSCI 105 S2 C-Assignment One 5 of 13 3. Change 4. Delete a contact 5. Display all contacts 6. Quit the program an existing contact Enter your choice: 2 Enter full name:Harry Enter phone number: 8873513 Enter email address: harryêemail.com Harry is added to the phone book. 1. Look up a contact 2. Add a new contact 3. Change an existing contact 4. Delete a contact 5. Dis 6. Quit the program play ali contacts Enter your choice: 2 Enter full name: Nicky Nicky is already in the phone book 1. Look up a contact 2. Add a new contact 3. Change an existing contact 4. Delete a contact 5. Display all c contacts 6. Quit the program Enter choice: To achieve the abore task you should implement the add a_record () method in the PhoneBook class. The add_a_record (self) fulfills the function of adding a new contact record by user input The function requests for an user input name, then it makes use of the find_a_record0 to determine whether the input name already exists in the phone book or not If no matching name is found, it will further asking input for the phone number and email address, then add a newContact object to the phone book. Otherwise, if the input name already exists in the phone book it prints out the error message. The sample input output are shown in the above example.Explanation / Answer
The following python program will cater to your needs.It has both the methods as given in the question:
import os
class Phonebook(object):
def __init__(self):
self.Name=[]
self.Phone=[]
self.Email=[]
print("----------------------------------------------")
print("--A Phone Book Management Program by abcd001--")
self.showPref()
def showPref(self):
while(True):
print("----------------------------------------------")
print("1. Look up a contact")
print("2. Add a new contact")
print("3. Change an existing contact")
print("4. Delete a contact")
print("5. Display all contacts")
print("6. Quit the program")
print("----------------------------------------------")
pref=input("Enter your choice: ")
if pref=='2':
self.add_a_record()
elif pref=='6':
os._exit(0)
def find_a_record(self,name):
if name in self.Name:
return True
else:
return False
def add_a_record(self):
print("----------------------------------------------")
name=input("Enter a full name:")
if not self.find_a_record(name):
phone=input("Enter phone number:")
email=input("Enter email address:")
self.Name.append(name)
self.Phone.append(phone)
self.Email.append(email)
print(name," is added to the phone book.")
else:
print(name," is already in the phone book.")
self.showPref()
MyRegistry=Phonebook()
MyRegistry()
Hope that helps :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.