I am following this exercise I found online:
http://gsl.mit.edu/media/programs/sri-lanka-summer-2012/materials/t_hw1.pdf
It will give a more understanding of what I try to accomplish.
When I call on the search_contact
on lastname
<-- (Simpson) function I do get the first object:
Homer, Simpson, -- Phone Number: 5559355899, --Email: homer@mail.com
But I want to fetch the second object also, so the output would be like so:
Homer, Simpson, -- Phone Number: 5559355899, --Email: homer@mail.com
Marge, Simpson, -- Phone Number: 5559352365, --Email: marge@mail.com
Still practicing on grasping the object oriented programming paradigm. Let me know if something is not clear. Appreaciate your help, folks.
This is what I have so far:
class Person:
def __init__(self, firstname, lastname, phone_number, email):
self.firstname= firstname
self.lastname= lastname
self.phone_number = phone_number
self.email = email
def __str__(self):
template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(self.firstname, self.lastname, self.phone_number, self.email)
template = template.replace('[','').replace(']','').replace("'", '')
return template.format(self)
class AddressBook(Person):
def __init__(self):
self.book = {}
def add_contact(self, p):
self.book[p] = p
return self.book
def search_contact(self, lastname):
for p in self.book:
if p.lastname == lastname:
template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(p.firstname, p.lastname, p.phone_number, p.email)
return template
if __name__ == '__main__':
# Bob = Person('Bob', 'Lop', '5559358150', 'bob@mail.com)
# print(Bob)
# Joe = Person('Joe', 'Roe', '5551940325',['joe@mail.com', 'roe@mail.com'])
# print(Joe)
a = AddressBook()
added = a.add_contact(Person('Homer', 'Simpson', '5559355899', 'homer@mail.com'))
# print(added)
added_1 = a.add_contact(Person('Marge', 'Simpson', '5559352365', 'marge@mail.com'))
# print(added_1)
search = a.search_contact('Simpson')
print(search)