python dict

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
1
Indexable
Never
phonebook = {}
phonebook[1] = {"name": "Winston", "number": "1234567890"}

interface = str("0")
while interface != "3":
    interface = input("Enter command. 1: search, 2: add, 3: quit \n")
    if interface == "1":
        namePlaceholder = input("Enter the name you want to search (i.e Winston): ")
        numberResult = str("0")
        print("Now searching for entries... \n")
        for entry in phonebook:
            if phonebook[entry]["name"] == namePlaceholder:
                numberResult = phonebook[entry]["number"]
        if numberResult == "0":
            print("Cannot find any entry with that name \n")
        else:
            print("Number: " + numberResult)
    elif interface == "2":
        namePlaceholder = input("Enter the name (i.e Winston): ")
        numberPlaceholder = input("Enter the number (i.e 12345667890): ")
        while numberPlaceholder.isdigit() == False:
            print("Phone number only contains number! Please re-input \n")
            numberPlaceholder = input("Enter the number (i.e 12345667890): ")
        phonebook[len(phonebook) + 1] = {
            "name": namePlaceholder,
            "number": numberPlaceholder,
        }
    else:
        print("Command only accept input as 1: search, 2: add, 3: quit \n")
print("Program closes")