# A program that implements a rudimentary phone catalog: # - Asks the user to enter enter names and phone numbers. # - Enters in a loop where the user enters a name and the # program looks up and prints out the corresponding phone number. # This is an variation of phone_numbers_1.py, that behaves mostly the same way. # One difference is that here the information is stored in a single list # of pairs, as opposed to a pair of lists. Another difference is that # this program prints a message when the name is not found in the catalog. # initialize the list will store names and phone numbers # convention: catalog[i] will store a list of two elements: position # 0 will store the name, and position 1 will store the phone number. catalog = [] # get from the user the names and phone numbers. while True: name = input("enter a name: ") if (name == "q") or (name == "quit"): break number = input("enter a phone number for " + name + ": ") catalog.append([name, number]) print() # now, get in a loop where the user enters a name, and the computer # looks up and prints out the corresponding phone number. while True: name = input("enter a name to look up: ") if (name == "q") or (name == "quit"): break name_found = False for entry in catalog: if name == entry[0]: print("the phone number of", name, "is", entry[1], "\n") name_found = True if (name_found == False): print("Name ", name, "is not in this catalog.\n")