Day 24 - Contact List Using Custom Functions and a Dictionary Object

Contact List with Add, Remove, List functions. We will use custom functions and begin to play with dictionary objects.

Finished Code

diPhone = {"Bill":"111-111-1111",
           "Thomas":"222-222-2222"}

def addContact(diData):
    strName = input("What is the name? ").strip()

    if strName in diData:
        print("That name already exists in the book.")
    else:
        strPhone = input("What is the phone? ").strip()
        diData[strName] = strPhone
        print(f"You have added {strName} to the list.")

        listContacts(diData)

    return diData

def listContacts(diData):
    print("\nHere are your contacts:")
    for strName, strNumber in diData.items():
        print(f"{strName}".ljust(15)+strNumber)

def removeContact(diData):
    listContacts(diData)
    strName = input("Enter the name you would like to remove: ")
    if strName in diData:
        del diData[strName]
        print(f"You have removed {strName}.")
    else:
        print(f"{strName} was not found in your contacts.")

    return diData


print("Welcome to Contacts List:")

while True:
    print("""\nMain Menu. 
    Choose one of the following options:
    1. List Contacts
    2. Add a Contact
    3. Remove a Contact
    4. Exit the program\n""")
    strMenu = input("What do you want to do? ")

    if (strMenu == "1"):
        listContacts(diPhone)
    elif (strMenu == "2"):
        diPhone = addContact(diPhone)
    elif (strMenu == "3"):
        diPhone = removeContact(diPhone)
    elif (strMenu == "4"):
        print("Thank you for using Contacts List")
        break
    else:
        print("That isn't an option.\n")