Question: Python code. Make it so that when you add a new contact, when the correctness of the entered data is checked and there are exceptions,

Python code.

Make it so that when you add a new contact, when the correctness of the entered data is checked and there are exceptions, the program asks you to enter the data again until they are entered correctly. This should work line-by-line(that is, for each line of data entry).After all the data is entered correctly, the contact should be saved to the dictionary and the user should receive messages that the contact is saved.

Program code to change:

import re # regular expression used for validating email import pickle # since this program reading object from a file initially so it is expected that pickle object file stored # in the same directory # name_of_file: data_file.pickle # program is case sensitive class Phone_book: # it is a static variable which is dictionary and contain all contact which is unpickle all_contacts = {} # since name is acting as a primary key here so it must be unique as well as whenever a new object # will create then it supposed that name has been passed in constructor itself. def __init__(self, new_name): self._name = '' self._address = '' self._email = '' self._phone = '' # now we need to add name in our dictionary along with its object as key value pair self.name = new_name Phone_book.all_contacts[self.name] = self # setters and getters for name @property def name(self): return self._name @name.setter def name(self, new_name): # first we need to check whether new_name is unique or not if new_name in Phone_book.all_contacts.keys(): raise Exception(" This name already exist in Database! ") # now we'll validate whether name is totally string or not if new_name.isalpha(): self._name = new_name else: raise ValueError(" Name must contain letters only!") # setter and getter for phone @property def phone(self): return self._phone @phone.setter def phone(self, new_phone): # check whether phone contain only number or not if new_phone.isdigit(): self._phone = new_phone else: raise ValueError(" Phone number must contain numeric digits! ") # setter and getter for address @property def address(self): return self._address @address.setter def address(self, new_address): self._address = new_address # setter and getter for address @property def email(self): return self._email @email.setter def email(self, new_email): # using regular expression to validate email regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' # for custom mails use: '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w+$' if (re.search(regex, new_email)): self._email = new_email else: raise ValueError(" Invalid email format!") # it will serialize(write) dict into pickle object file @staticmethod def serialize(): with open("data_file.pickle", 'wb') as f: pickle.dump(Phone_book.all_contacts, f) @staticmethod def deserialize(): try: with open("data_file.pickle", 'rb') as f: Phone_book.all_contacts = pickle.load(f) except FileNotFoundError: print(" Pickle object file might not be created yet! ") if __name__ == "__main__": # When running the program should unpickle the file to retrieve the dictionary # this function will read the objects from pickle object file and add into all_contact dictionary """ 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 """ Phone_book.deserialize() # designing the console # initializing choice choice = '0' while choice != '6': print(" Select a choice:- 1: Find Contact by Name 2: Add a new contact 3: Edit email of an existing contact" " 4: Change an existing phone number 5: Delete a contact 6: Exit ") choice = input("Enter your choice: ").strip() # strip used to wipe out accidental spaces # Find a Contact by name and show information about it if choice == '1': search_name = input("Enter name to be searched: ").strip() # try to find out whether name exist in dictionary or not current_obj = Phone_book.all_contacts.get(search_name) # if search_name exist in dictionary then it will return something if current_obj: print(" _________________________Contact Details of %s_________________________"%search_name) print("Name : ", current_obj.name) print("Phone : ", current_obj.phone) print("Email : ", current_obj.email) print("Address: ", current_obj.address) else: print(" Name doesn't exist in database! Try again... ")  # Add a new contact and information about it elif choice == '2': # if there is any error then it should be handled try: print("Enter following details!") new_name = input("Name: ").strip() new_email = input("Email: ").strip() new_phone = input("Phone: ").strip() new_address = input("Address: ").strip() new_object = Phone_book(new_name) new_object.email = new_email new_object.phone = new_phone new_object.address = new_address print(" New contact with name %s added successfully! "%new_name) except Exception as ex: print(ex) # Edit an existing email elif choice == '3': # first we need to identify the person whose email is going to change name = input("Enter name of person whose email to be change: ").strip() current_obj = Phone_book.all_contacts.get(name) if current_obj: # it may possible that new email doesn't meet the syntax try: new_email = input("Enter new email: ").strip() current_obj.email = new_email print(" Email updated successfully! ") except Exception as ex: print(ex) else: print(" Person with name %s doesn't exist! Try again "%name) # Change an existing phone number elif choice == '4': # first we need to identify the person whose phone is going to change name = input("Enter name of person whose phone to be change: ").strip() # get the object first current_obj = Phone_book.all_contacts.get(name) if current_obj: # it may possible that new email doesn't meet the syntax try: new_phone = input("Enter new phone: ").strip() current_obj.phone = new_phone print(" Phone number updated successfully! ") except Exception as ex: print(ex) else: print(" Person with name %s doesn't exist! Try again " % name) # Delete a contact and all information about it elif choice == '5': # first we need to identify the person name = input("Enter name of person to be deleted: ").strip() # get the object first current_obj = Phone_book.all_contacts.get(name) # checking whether object exist or not if current_obj: # taking confirmation one more time conf = input("Are you sure? (Y/N): ") if conf == 'Y' or conf == 'y': Phone_book.all_contacts.pop(name) print(" Contact with name %s removed successfully! "%name) else: print(" Contact haven't deleted! ") else: print(" Person with name %s doesn't exist! Try again " % name) elif choice == '6': print(" ____________________Have a nice day!____________________ ") break else: print(" Invalid Choice! ") # this used for only testing purpose print(Phone_book.all_contacts) # 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). Phone_book.serialize()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!