# A program that implements a rudimentary phone catalog: # - First asks the user to specify the number of entries in the catalog. # - Second asks the user to enter enter names and phone numbers. # - Third, 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_2.py, that behaves the same way. # The difference is that here the information is stored in a single list # of pairs, as opposed to a pair of lists. # get number of entries N = input("enter number of entries: ") # 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. for i in range(0, N): name = raw_input("enter a name: ") number = raw_input("enter a phone number for " + name + ": ") catalog.append([name, number]) # 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 = raw_input("enter a name to look up: ") if name == "q": break name_found = False for entry in catalog: if name == entry[0]: print "the phone number of", name, "is", entry[1] name_found = True if (name_found == False): print "Name ", name, "is not in this catalog"