1

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)
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Niknak
  • 583
  • 1
  • 8
  • 22

1 Answers1

1

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

returns.

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', 
                                 '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')
    for s in search:
        print(s)

Output:

Simpson, Homer, -- Phone Number: 5559355899, --Email: homer@mail.com
Simpson, Marge, -- Phone Number: 5559352365, --Email: marge@mail.com
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • I tried with `yield` which gave me this in output `` is there somehow this memory placement can be converted to readable output? This is something new to me so sorry for asking =p – Niknak Nov 04 '18 at 18:47
  • 1
    `class AddressBook(Person):` or `class AddressBook:`? – eyllanesc Nov 04 '18 at 18:53
  • @eyllanesc the latter - there is no reason whatsoever to inherit here, it was pure copy/paste and evaded me – Patrick Artner Nov 04 '18 at 18:57
  • @Niknak you can `list(your_generator)` or iterate over it with `for someting in generatorname:` - use a list if its new and read about it here: [understanding generators in python](https://stackoverflow.com/questions/1756096/understanding-generators-in-python) – Patrick Artner Nov 04 '18 at 18:58
  • @PatrickArtner Ahhh, I see, thanks alot and do continue to have a wonderful evening! =D – Niknak Nov 04 '18 at 19:02