Mar-05-2020, 11:34 PM
I'm stuck. I have no idea what I did wrong but, basically, I need to create a temporary text file or something? Something to save the emails after I end to program so I can come back to it and look up the email. Here's the description of the homework.
Quote:Write a program that keeps names and addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a person's email address, add a new name and email address, change an existing email address, and delete an existing name and email address. Each time the program starts, it should retrieve the dictionary from the file and unpickle it.
""" nameandemail.py uses a penu system to maintain and manage email adresses author: Inge Teleky 2/24/20 """ import pickle def main(): filename = input("Enter your email file name: ") emails = load_emails(filename) choice = get_choice() while choice != 5: if choice == 1: look_up(emails) elif choice == 2: add(emails) elif choice == 3: change(emails) elif choice == 4: delete(emails) elif choice == 5: save_emails(filename) print("Information saved.") else: print("Invalid choice. Please enter 1-5 for your choice.") choice = get_choice() def load_emails(filename): """ read name and email into a dictionary :param filename: binary file than contains name and email address :return: """ try: input_file = open(filename, 'rb') #unpickle the file email_dictionary = pickle.load(input_file) input_file.close() except IOError: email_dictionary = {} return email_dictionary def get_choice(): print("Email Book Management") print("1. Look up an email.") print("2. Add a new name and email.") print("3. Change an existing email.") print("4. Delete a name and email.") print("5. Quit the program") choice = int(input("Enter your choice, 1-5: ")) return choice def add(emails): """ :param emails: dictionary of naame and email :return: updated emails and dictionary """ #ask user for name and email name = input("Enter name: ") email = input("Enter email: ") if name not in emails: emails[name] = email else: print("The name already exists") return emails def look_up(emails): name=input("Enter name: ") if name in emails: print(name, ": ", emails[name]) else: print("The specified name does not exist.") def change(emails): name = input("Enter the name of the email you wish to change: ") if name in emails: change = input("What do you wish to change the email to? ") emails[name] = change return emails def delete(emails): name = input("Enter the name of the email you wish to delete. ") if name in emails: delete = input("Are you sure you wish to delete this email? Y or y for yes. Email name: " + name) if delete == 'y' or delete == 'Y': emails.pop(name) else: pass return emails def save_emails(filename): with open(filename) as name: pickle.dump(filename, name, pickle.HIGHEST_PROTOCOL) main()