Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have submitted python code below for a personal contact book with classes. I have a few questions. 1. How can I display the contact

I have submitted python code below for a personal contact book with classes. I have a few questions. 1. How can I display the contact book like this: name of the owner, then the list of contacts then a blank line at the end. Contacts of "owner" Lastname, firstname, email, number Lastname, firstname, email, number (blank line) 2. How can I make it that it should update by first and last name, not just one or the other? 3. What would be the function for sorting the contacts: (i) by name (alphabetic by last name, then first name), (ii) by email address, (iii) by phone number. 4. When I try to remove a contact, if the contact is not found in the loop, it will remove contact index = -1 ie the last contact in the list. This causes it to always remove a contact, possibly the wrong one. How can I change that? 5. How can I validate the phone number? Thank you so much!!! 
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 "{} {}=> {},{}".format(self.lastname, self.firstname, self.email, self.phone) class PersonalContactBook: def __init__(self, owner): self.owner = owner self.contacts = [] def addContact(self, lastname, firstname, email, phone): lastname = lastname.strip().title() firstname = firstname.strip().title() update_phone = str(phone) update_phone = update_phone[:3] + update_phone[3:6] + update_phone[6:] assert "@" in email, "@ not present in Email" assert email[-4:] in (".com", ".edu", ".gov", ".net"), "Invalid domain" con = ContactItem(lastname, firstname, email, phone) self.contacts.append(con) def updateContact(self, lastname, firstname, update_to, update_value): for contact in self.contacts: if contact.lastname == update_to: contact.lastname = update_value elif contact.firstname == update_to: contact.firstname = update_value elif contact.email == update_to: contact.email = update_value elif contact.phone == update_to: contact.phone = update_value 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): index = -1 for ind, contact in enumerate(self.contacts): if contact.firstname == firstname and contact.lastname == lastname: index = ind break self.contacts.pop(index) def displayContacts(self): for contact in self.contacts: print(contact) def sortContacts(self): self.contacts.sorted(key=lambda x: x[0]) 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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions