from number_check import * """ show_menu: input: none output: the numerical value that the user has entered description: This function prints out a menu for the user to see and gets (and returns) a numerical choice from the user. """ def show_menu(): print("1: make new entry") print("2: modify existing entry") print("3: delete an entry") print("4: look up a name") print("5: look up a number") print("6: print entire phonebook") print("7: quit") while True: user_choice = get_integer("please select a choice (from 1 to 7): ") if (user_choice >= 1) and (user_choice <= 7): return user_choice """ make_new_entry input: none output: none description: this function asks the user to specify a new entry (new name and phone number) and stores that in the phonebook More specifically, this function: 1: reads the phonebook from a file 2: gets the new name and number from the user 3: adds the name and number to the phonebook 4: saves the phonebook to a file """ def make_new_entry(): phonebook = read_phonebook() name = input("please enter a name: ") number = input("please enter a number: ") phonebook = add_entry(phonebook, name, number) save_phonebook(phonebook) """ save_phonebook input: a phonebook output: none description: this function saves the phonebook to a file """ def save_phonebook(phonebook): out_stream = open('phonebook.txt', 'w') for entry in phonebook: name = entry[0] number = entry[1] print(name, file=out_stream) print(number, file=out_stream) out_stream.close() """ read_phonebook input: none output: a phonebook description: this function reads the phonebook from a file and returns the phonebook. """ def read_phonebook(): input_file = open("phonebook.txt", "r") lines = input_file.readlines() input_file.close() phonebook = [] for position in range(0, len(lines), 2): name = lines[position].strip() number = lines[position+1].strip() phonebook.append([name, number]) return phonebook """ read_phonebook input: a phonebook, a name, and a number output: a phonebook description: this function changes the phonebook, by adding to it a new entry, with the specified name and number. The function returns the modified phonebook. """ def add_entry(phonebook, name, number): phonebook.append([name, number]) return phonebook """ print_phonebook input: a phonebook output: none description: this function prints the entries in the phonebook. """ def print_phonebook(phonebook): print() for entry in phonebook: name = entry[0] number = entry[1] print(name + ":", number) print() """ find_entry_by_name input: a phonebook and a name (string) output: an entry (list of two strings, a name and a number), or None if no entry was found. description: this function searches the phonebook for an entry (i.e., a pair of a name and a phone number) that matches the specified name. If it finds such a matching entry, it returns it. Otherwise, it returns None. """ def find_entry_by_name(phonebook, name): for entry in phonebook: if (entry[0].lower() == name.lower()): return entry return None """ modify_entry input: none output: none description: this function allows the user to modify an existing entry in the phone book. First, the function reads the phonebook from a file. Then, the function asks the user to specify an entry to be modified, by providing a name. Then, if the function cannot find a matching entry, it prints a message to the user. If it does find the entry, it asks the user for the modified name and number. Finally, the function makes the requested change in the phonebook, and saves the phonebook to a file. """ def modify_entry(): phonebook = read_phonebook() name = input("enter name of entry to be modified: ") entry = find_entry_by_name(phonebook, name) if (entry == None): print("\n***** Could not find an entry with that name\n") return new_name = input("enter new name (ENTER for no change)") new_number = input("enter new number (ENTER for no change)") if new_name.strip() != "": entry[0] = new_name if new_number.strip() != "": entry[1] = new_number save_phonebook(phonebook) """ main description: this is the starting point for the program. The program enters a loop, where it keeps showing a menu to the user, getting a choice from the user, and responding to that choice. If the user chooses the option for quitting the program, then the program exits. """ def main(): while True: choice = show_menu() if (choice == 1): make_new_entry() elif (choice == 2): modify_entry() elif (choice == 6): phonebook = read_phonebook() print_phonebook(phonebook) elif (choice == 7): return main()