Question: 1.How can I make it that the program will not allow me to add or update a contact with the same last name as another
1.How can I make it that the program will not allow me to add or update a contact with the same last name as another contact? (two parts, adding and updating) 2. How can I make it then when finding and deleting a contact, it will work even if user does not input name capitalized? 3. I can't figure out how to make it that by updating a contact, user can choose by lastname, firstname which contact to update? Thank you!! #Personal Contact book Python import operator class ContactItem: def __init__(self, lastname, firstname, email, phone): self.lastname = lastname.strip().title() self.firstname = firstname.strip().title() self.email = email self.phone = phone def __str__(self): return "{:<15} {:<10} {:<25} {:<20}".format(self.lastname, self.firstname, self.email, self.phone) class PersonalContactBook: def __init__(self, owner): self.owner = owner.strip().title() self.contacts = [] def addContact(self, lastname, firstname, email, phone): lastname = lastname.strip().title() firstname = firstname.strip().title() email = email.strip().lower() assert "@" in email, "@ not present in Email" assert email[-4:] in (".com", ".edu", ".gov", ".net", ".org"), "Invalid domain" phone = phone.strip() firstslot = phone[:3] secondslot = phone[4:7] thirdslot = phone[8:] if len(phone) != 12: print("Invalid phone number. Must be in xxx-yyy-zzzz format") exit() elif not firstslot.isdigit() or not secondslot.isdigit() or not thirdslot.isdigit(): print("Invalid phone number. Must be in xxx-yyy-zzzz format") exit() elif phone.count('-') != 2: print("Invalid phone number. Must be in xxx-yyy-zzzz format") exit() elif phone.index('-') != 3 or phone.rindex('-') != 7: print("Invalid phone number. Must be in xxx-yyy-zzzz format") exit() con = ContactItem(lastname, firstname, email, phone) self.contacts.append(con) def updateContact(self, lastname, firstname, original_value, update_value): for contact in self.contacts: if contact.lastname==lastname and contact.firstname==firstname: if contact.lastname == original_value: contact.lastname = update_value.strip().title() elif contact.firstname == original_value: contact.firstname = update_value.strip().title() elif contact.email == original_value: assert "@" in contact.email, "@ not present in Email" assert contact.email[-4:] in (".com", ".edu", ".gov", ".net", ".org"), "Invalid domain" contact.email = update_value.lower() elif contact.phone == original_value: firstslot = contact.phone[:3] secondslot = contact.phone[4:7] thirdslot = contact.phone[8:] if len(update_value) != 12: print("Invalid phone number. Must be in xxx-yyy-zzzz format") exit() elif not firstslot.isdigit() or not secondslot.isdigit() or not thirdslot.isdigit(): print("Invalid phone number. Must be in xxx-yyy-zzzz format") exit() elif update_value.count('-') != 2: print("Invalid phone number. Must be in xxx-yyy-zzzz format") exit() elif update_value.index('-') != 3 or update_value.rindex('-') != 7: print("Invalid phone number. Must be in xxx-yyy-zzzz format") exit() else: contact.phone = update_value print(contact) print("Not Found") def findContact(self, lastname, firstname): for contact in self.contacts: if contact.firstname == firstname and contact.lastname == lastname: print(contact) return print("Not Found") def removeContact(self, lastname, firstname): found = False index = -1 for ind, contact in enumerate(self.contacts): if contact.firstname == firstname and contact.lastname == lastname: found = True index = ind break if found == True: self.contacts.pop(index) else: print("Contact not found.") def displayContacts(self): print("Contacts of", self.owner) for contact in self.contacts: print(contact) print() def sortByName(self): self.contacts = sorted(self.contacts, key=operator.attrgetter("lastname", "firstname")) def sortByEmail(self): self.contacts = sorted(self.contacts, key=operator.attrgetter("email")) def sortByPhone(self): self.contacts = sorted(self.contacts, key=operator.attrgetter("phone")) if __name__ == "__main__": pcb1 = PersonalContactBook("owner1") pcb2 = PersonalContactBook("owner2") pcb3 = PersonalContactBook("owner3") pcb4 = PersonalContactBook("owner4") Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
