SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Dictionaries And Sets
Exercise:
Programming Exercises
Question:8 | ISBN:9780132576376 | Edition: 2

Question

Name and Email Addresses 
Write a program that keeps names and email 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. The program should pickle the dictionary and save it to a file when the user exits the program. Each time the program starts, it should retrieve the dictionary from the file and unpickle it.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Name and Email Address Program code:

import pickle

def load_dictionary(file_path):
    try:
        with open(file_path, 'rb') as file:
            dictionary = pickle.load(file)
    except FileNotFoundError:
        dictionary = {}
    
    return dictionary

def save_dictionary(dictionary, file_path):
    with open(file_path, 'wb') as file:
        pickle.dump(dictionary, file)

def display_menu():
    print("Menu:")
    print("1. Look up a person’s email address")
    print("2. Add a new name and email address")
    print("3. Change an email address")
    print("4. Delete a name and email address")
    print("5. Exit")

def look_up_email(dictionary):
    name = input("Enter the name to look up: ")
    if name in dictionary:
        email = dictionary[name]
        print("Email address for {} is: {}".format(name, email))
    else:
        print("Name not found.")

def add_new_entry(dictionary):
    name = input("Enter the name: ")
    email = input("Enter the email address: ")
    dictionary[name] = email
    print("Name and email address added.")

def change_email(dictionary):
    name = input("Enter the name for which you want to change the email address: ")
    if name in dictionary:
        new_email = input("Enter the new email address: ")
        dictionary[name] = new_email
        print("Email address changed.")
    else:
        print("Name not found.")

def delete_entry(dictionary):
    name = input("Enter the name to delete: ")
    if name in dictionary:
        del dictionary[name]
        print("Name and email address deleted.")
    else:
        print("Name not found.")

# File path to store the dictionary
file_path = "dictionary.pickle"

# Load the dictionary from the file
dictionary = load_dictionary(file_path)

# Main program loop
while True:
    display_menu()
    choice = input("Enter your choice (1-5): ")

    if choice == "1":
        look_up_email(dictionary)
    elif choice == "2":
        add_new_entry(dictionary)
    elif choice == "3":
        change_email(dictionary)
    elif choice == "4":
        delete_entry(dictionary)
    elif choice == "5":
        # Save the dictionary to the file and exit
        save_dictionary(dictionary, file_path)
        print("Dictionary saved. Exiting the program.")
        break
    else:
        print("Invalid choice. Please try again.")

Executed Output:

Menu:
1. Look up a person’s email address
2. Add a new name and email address
3. Change an email address
4. Delete a name and email address
5. Exit
Enter your choice (1-5): 1
Enter the name to look up: Krish
Name not found.
Menu:
1. Look up a person’s email address
2. Add a new name and email address
3. Change an email address
4. Delete a name and email address
5. Exit
Enter your choice (1-5): 2
Enter the name: Charless
Enter the email address: Charless@gmail.com
Name and email address added.
Menu:
1. Look up a person’s email address
2. Add a new name and email address
3. Change an email address
4. Delete a name and email address
5. Exit
Enter your choice (1-5): 2
Enter the name: Krish
Enter the email address: Krish@gmail.com
Name and email address added.
Menu:
1. Look up a person’s email address
2. Add a new name and email address
3. Change an email address
4. Delete a name and email address
5. Exit
Enter your choice (1-5): 3
Enter the name for which you want to change the email address: Krish
Enter the new email address: Krish123@gmail.com
Email address changed.
Menu:
1. Look up a person’s email address
2. Add a new name and email address
3. Change an email address
4. Delete a name and email address
5. Exit
Enter your choice (1-5): 1
Enter the name to look up: Krish
Email address for Krish is: Krish123@gmail.com
Menu:
1. Look up a person’s email address
2. Add a new name and email address
3. Change an email address
4. Delete a name and email address
5. Exit
Enter your choice (1-5): 4
Enter the name to delete: Krish
Name and email address deleted.
Menu:
1. Look up a person’s email address
2. Add a new name and email address
3. Change an email address
4. Delete a name and email address
5. Exit
Enter your choice (1-5): 1
Enter the name to look up: Krish
Name not found.
Menu:
1. Look up a person’s email address
2. Add a new name and email address
3. Change an email address
4. Delete a name and email address
5. Exit
Enter your choice (1-5): 5
Dictionary saved. Exiting the program.

 

0 0

Discussions

Post the discussion to improve the above solution.