Gavin O'Doherty
Gavin O'Doherty

Reputation: 61

Why doesn't the dictionary lookup work in my Python phone book program?

I wish to create a Phone Book System in Python as per image below which allows users to add, delete, update and lookup phone numbers.

enter image description here

Please see my code below. Options 1,2,3 and 5 work fine, however, option 4 is not working and I'm not sure why. When I add a new entry using option 1 and then immediately try to look up the entry using option 4, it tells me that the entry does not exist when it clearly does.. Am I missing something? (section of code in question starts with "elif selection == 4:")

def intro():
    welcome = ("                                     \n"
               "#####################################\n"
               "MYPY PHONE BOOK\n"
               "#####################################\n"
               "1 : Add New Entry\n"
               "2 : Delete Entry\n"
               "3 : Update Entry\n"
               "4 : Lookup Number\n"
               "5 : QUIT\n"
               "")
    selection = input(welcome)
    return int(selection)

phoneBook = {}

while True:
    selection = intro()
    if selection == 1:
        number = input("Enter phone number: ")
        name = input("Enter name: ")
        if number in phoneBook:
            print("                               \n"
                  "* Number already in phonebook *")
        else:
            phoneBook.update({number : name})
            print("                               \n"
                  "* Name and number added to phone book *")
            print(phoneBook)
    elif selection == 2:
        number2 = input("Enter phone number to be deleted: ")
        if number2 in phoneBook:
            phoneBook.pop(number2)
            print("                               \n"
                  "* Number deleted from phone book *")
            print(phoneBook)
        else:
            print("                               \n"
                  "* Number does not exist in phone book *")
    elif selection == 3:
        number3 = input("Enter current phone number: ")
        numberNew = input("Enter updated phone number: ")
        nameNew = input("Enter name: ")
        if number3 in phoneBook:
            phoneBook.pop(number3)
            phoneBook.update({numberNew : nameNew})
            print("                               \n"
                  "* Name and number updated in phone book *")
            print(phoneBook)
        else:
            print("                               \n"
                  "* Number does not exist in phone book *")
    elif selection == 4:
        name4 = input("Enter name: ")
        if name4 in phoneBook:
            for key,value in phoneBook.items():
                if value == name4:
                    print(key)
        else:
            choice = int(input("                                 \n"
                               "Name does not exist in phone book\n"
                               "Enter 1 to add to phoneBook\n"
                               "Enter 2 to return to the menu:\n"
                               ""))
            if choice == 1:
                number4 = input("Enter phone number: ")
                name44 = input("Enter name: ")
                if number in phoneBook:
                    print("                               \n"
                          "* Number already in phonebook *")
                else:
                    phoneBook.update({number4 : name44})
                    print("                               \n"
                          "* Name and number added to phone book *")
                    print(phoneBook)
            elif choice == 2:
                print("                                \n"
                      "* Please make another selection *")
    elif selection == 5:
        break

Upvotes: 0

Views: 553

Answers (1)

Are you using names as keys or numbers as keys of your dictionary?

In selection 4, you do if name4 in phoneBook:..., however, in other parts of the code, you seem to use the numbers as keys. With dictionaries, you can check if a key is in a dictionary with key in dictionary. On the other hand, if you want to check if a value is in a dictionary, you can do:

for val in dictionary.values(): 
    if val == value:
        ...

Check it out ;)

Upvotes: 1

Related Questions