Reputation: 593
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: [email protected]
But I want to fetch the second object also, so the output would be like so:
Homer, Simpson, -- Phone Number: 5559355899, --Email: [email protected]
Marge, Simpson, -- Phone Number: 5559352365, --Email: [email protected]
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', '[email protected])
# print(Bob)
# Joe = Person('Joe', 'Roe', '5551940325',['[email protected]', '[email protected]'])
# print(Joe)
a = AddressBook()
added = a.add_contact(Person('Homer', 'Simpson', '5559355899', '[email protected]'))
# print(added)
added_1 = a.add_contact(Person('Marge', 'Simpson', '5559352365', '[email protected]'))
# print(added_1)
search = a.search_contact('Simpson')
print(search)
Upvotes: 1
Views: 29
Reputation: 51643
You have to fix an error in your Person,__init__()
- the order of parameters is incorrect (or provide the names flipped when you create the Person(...)
).
This
def search_contact(self, lastname): for p in self.book: if p.lastname == lastname: template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(p.lastname, p.firstname, p.phone_number, p.email) return template
return
s.
If you return
from a function you are done. You can either yield
(making it a generator) or collect all matches in a list
and return
it. Either way you need to adapt your print-code because now you get a list/generator returned.
Example for a list (generator are more complex):
class Person:
def __init__(self, firstname, lastname, phone_number, email): # FIX
self.lastname = lastname
self.firstname = firstname
self.phone_number = phone_number
self.email = email
def __str__(self):
template = '{}, {}, -- Phone Number: {}, --Email: {}'.format(
self.lastname, self.firstname, self.phone_number, self.email)
template = template.replace('[','').replace(']','').replace("'", '')
return template.format(self)
class AddressBook:
def __init__(self):
self.book = {}
def add_contact(self, p):
self.book[p] = p
return self.book
def search_contact(self, lastname):
hits = []
for p in self.book:
if p.lastname == lastname:
hits.append(
'{}, {}, -- Phone Number: {}, --Email: {}'.format(
p.lastname, p.firstname, p.phone_number, p.email))
return hits
if __name__ == '__main__':
a = AddressBook()
added = a.add_contact(Person('Homer', 'Simpson', '5559355899',
'[email protected]'))
# print(added)
added_1 = a.add_contact(Person('Marge', 'Simpson', '5559352365',
'[email protected]'))
# print(added_1)
search = a.search_contact('Simpson')
for s in search:
print(s)
Output:
Simpson, Homer, -- Phone Number: 5559355899, --Email: [email protected]
Simpson, Marge, -- Phone Number: 5559352365, --Email: [email protected]
Upvotes: 1