Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How to store contacts that has been added, so that they can be checked for duplicate contacts already stored?

I want to make a method within a class that does the following: It should allow the user to add a new contact. This I got covered. Further, the method should not let the user add duplicate contacts. Two contacts are duplicate when all attributes are equal. So I need to find a way to store all contacts that has been added, so that each contact can be compared and checked. Is there a way to do this? this is what my code looks like:

class Phonebook:
    def addContact (self):


        name=str(input('enter name:'))
        lastname=str(input('enter lastname:'))
        number=int(input('enter number:'))

        contact_type=int(input('enter 1 for business contact and 2 for personal contact'))

        if contact_type==1: #add business contact
            company=str(input('enter company:'))
            businessAdress=str(input('enter businessAdress:'))
            #adding a contacts value:
            name=BusinessContact(name,lastname,number,company,businessAdress)

        elif contact_type==2:
            homeAdress=str(input('enter Homeadress'))
            relation=str(input('enter relation'))
            #adding a contacts value:
            name=BusinessContact(name,lastname,number,homeAdress,relation)   

        print('contact entered:')
        print(name.__dict__)

class Contact:
    def __init__ (self,name,surname,phone):
        self.name=name
        self.surname=surname
        self.phone=phone

class BusinessContact (Contact): #subclass. business contact are a contact.
    def __init__ (self,name,surname,phone,company,businessAdress):
        Contact.__init__(self,name,surname,phone)
        self.company=company
        self.businessAdress=businessAdress

class PersonalContact (Contact): #subclass.
    def __init__(self,name,surname,homeadress,realationship):
        Contact.__init__(self,name,surname,phone)
        self.homeadress=homeadress
        self.relationship=relationship


s=Phonebook()
s.addContact()

1 Answer

You should store all of your contacts in a collection and compare the new contact to the old ones. If none of them are the same, you can add that contact

yes I should. Do you know I can do that?

In general, you do that in your main() function every time you initialise a new instance of your class. But there might be some function or class property in python that allows you to do that.