Question
Code in Python Write a Python program for the phone book. The program must : 1) Store names and contact information in the dictionary as
Code in Python
Write a Python program for the phone book.
The program must:
1) Store names and contact information in the dictionary as key-value pairs.
2) Contact details must contain an object with attributes for address, email, and phone number
3)When running the program should unpickle the file to retrieve the dictionary
4) The console should display a menu with the following use cases:
1)Find a Contact by name and show information about it
2)Add a new contact and information about it
3)Edit an existing email
4)Change an existing phone number
5)Delete a contact and all information about it
6)Exit the phone book (in this case, first you must pickle the dictionary and save it to a file, and after finish executing the program).
5)When writing a program, replace setters and getters with properties
6)Make a check of the information entered by the user for correctness(that is, check that it has an ending with @ , the name consists only of letters , the phone number can only have numbers )
Code for the example:
import pickle
LOOK_UP = 1 ADD = 2 CHANGE = 3 DELETE = 4 QUIT = 5 FILENAME = 'contacts.dat'
class Contact: >>>>def __init__(self, name, phone, email): >>>>>>>>self._name = name >>>>>>>>self._phone = phone >>>>>>>>self._email = email
>>>>def set_name(self, name): >>>>>>>>self._name = name >>>>def set_phone(self, phone): >>>>>>>>self._name = phone >>>>def set_email(self, email): >>>>>>>>self._name = email >>>>def get_name(self, name): >>>>>>>>return self._name >>>>def get_phone(self, phone): >>>>>>>>return self._phone >>>> def get_email(self, email): >>>>>>>> return self._email >>>>def __str__(self): >>>>>>>> return "Name: " + self._name + \ >>>>>>>> " Phone: " + self._phone +\ >>>>>>>>" Email: " + self._email def main(): >>>>mycontacts = load_contacts() >>>>choice = 0 >>>>while choice != QUIT: >>>>>>>>choice = get_menu_choice() >>>>>>>>if choice == LOOK_UP: >>>>>>>>>>>>look_up(mycontacts) >>>>>>>>elif choice == ADD: >>>>>>>>>>>>add(mycontacts) >>>>>>>>elif choice == CHANGE: >>>>>>>>>>>>change(mycontacts) >>>>>>>>elif choice == DELETE: >>>>>>>>>>>>delete(mycontacts) >>>>save_contacts(mycontacts) def load_contacts():
>>>>try:
>>>>>>>>input_file = open(FILENAME, 'rb') >>>>>>>>contacts_dct = pickle.load(input_file) >>>>>>>>input_file.close() >>>>except IOError: >>>>>>>>contact_dct = {} >>>>return contact_dct def get_menu_choice(): >>>>print() >>>>print('Menu') >>>>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. Quit the program') >>>>print('') >>>>choice = int(input('Enter your choice: ')) >>>>while choice < LOOK_UP or choice > QUIT: >>>>>>>>choice = int(input('Enter a valid choice: ')) >>>>return choice def look_up(mycontacts): >>>>name = input('Enter a name: ') >>>>print(mycontacts.get(name, 'That name is not found.')) def add(mycontacts): >>>>name = input('Name: ') >>>>phone = input('Phone: ') >>>>email = input('Email: ') >>>>entry = Contact(name, phone, email) >>>>if name not in mycontacts: >>>>>>>>mycontacts[name] = entry >>>>>>>>print('The entry has been added.') >>>>else: >>>>>>>>print('That name already exists.') def change(mycontacts): >>>>name = input('Enter a name: ') >>>>if name in mycontacts: >>>>>>>>phone = input('Enter the new phone number: ') >>>>>>>>email = input('Enter the new email address: ') >>>>>>>>entry = contact.Contact(name, phone, email) >>>>>>>>mycontacts[name] = entry >>>>>>>>print('Information updated.') >>>>else: >>>>>>>>print('That name is not found') def delete(mycontacts): >>>>name = input('Enter a name: ') >>>> if name in mycontacts: >>>>>>>>del mycontacts[name] >>>>>>>>print('Entry delete.') >>>>else: >>>>>>>>print('That name is not found.') def save_contacts(mycontacts): >>>>output_file = open (FILENAME, 'wb') >>>>pickle.dump(mycontacts, output_file) >>>>output_file.close() main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started