Gennaro
Gennaro

Reputation: 113

Function is not adding information to my dictionary

So I've got my functions created like so:

def create_contact(contacts, first, last, email, age, phone):
   """
   Creates a contact.
   """

   contacts[(first, last)] = [email, age, phone]


def contains_contact(contacts, first, last):
    """
    Checks to see if the dictionary contains  a contact.
    """
    if (first.lower(), last.lower()) in contacts == True:
        return(True)
    else:
        return(False)

and then at the bottom of my code I have my main function like so:

def main():
   # The Dictionary
   contacts = {}
   # Provided Test Code
   create_contact(contacts, "Katie", "Katz", "[email protected]",
               25, "857-294-2758")
   #Checks to see if item in dictionary exists
   print("Creation of Katie Katz: {}".format(
    "Passed" if contains_contact(contacts, "Katie", "kaTz") else
    "Failed"))

I’m not sure where I’m going wrong. It seems as though the items are not being added to my dictionary. Unsure as to why, maybe I’m attempting this all wrong.

Upvotes: 1

Views: 58

Answers (1)

Georg Grab
Georg Grab

Reputation: 2301

The adding of items to your dict is working fine, however you forgot to convert the first and last name to lowercase in create_contact. You did do it in contains_contact however, which is why your code doesn't work.

def create_contact(contacts, first, last, email, age, phone):
   """
   Creates a contact.
   """

   contacts[(first.lower(), last.lower())] = [email, age, phone]

Additionally, your contains_contact function could be simplified:

def contains_contact(contacts, first, last):
    """
    Checks to see if the dictionary contains  a contact.
    """
    return (first.lower(), last.lower()) in contacts 

Upvotes: 1

Related Questions