Last active
November 9, 2016 00:08
-
-
Save s2t2/72bcce3bde5e3837801c5f46f9683e12 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pickle # reference: https://wiki.python.org/moin/UsingPickle | |
# | |
# Write a program that keeps names and email addresses in a dictionary as key-value pairs. | |
# Each time the program starts, it should retrieve the dictionary from the file and unpickle it. | |
# | |
ADDRESS_BOOK_FILE_NAME = os.path.join(os.path.dirname(__file__), "address_book.p") | |
if os.path.isfile(ADDRESS_BOOK_FILE_NAME): | |
address_book = pickle.load( open(ADDRESS_BOOK_FILE_NAME, "rb" ) ) #> {'lisa': '[email protected]', 'mike': '[email protected]'} | |
else: | |
address_book = {} | |
# | |
# 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 | |
# delete an existing name and email address | |
# | |
def handle_menu_choice(menu_choice): | |
print("YOU CHOSE: " + menu_choice) | |
if menu_choice == "Create": | |
handle_create() | |
elif menu_choice == "Read": | |
handle_read() | |
elif menu_choice == "Update": | |
handle_update() | |
elif menu_choice == "Destroy": | |
handle_destroy() | |
else: | |
print("TODO: implement handler for " + menu_choice) | |
def handle_create(): | |
name = input("Please input the person's name (e.g. 'Rich'):") | |
email = input("Please input the person's email (e.g. '[email protected]'):") | |
address_book[name] = email | |
def handle_read(): | |
name = input("Please input the person's name (e.g. 'Rich'):") | |
if name in address_book: | |
print("FOUND NAME " + name + " IN ADDRESS BOOK") | |
print("EMAIL ADDRESS IS " + address_book[name]) | |
else: | |
print("NAME " + name + " NOT FOUND IN ADDRESS BOOK") | |
def handle_update(): | |
name = input("Please input the person's name (e.g. 'Rich'):") | |
if name in address_book: | |
print("FOUND NAME " + name + " IN ADDRESS BOOK") | |
print("EMAIL ADDRESS IS " + address_book[name]) | |
email = input("Please input the person's new email (e.g. '[email protected]'):") | |
address_book[name] = email | |
else: | |
print("NAME " + name + " NOT FOUND IN ADDRESS BOOK") | |
def handle_destroy(): | |
name = input("Please input the person's name (e.g. 'Rich'):") | |
if name in address_book: | |
print("FOUND NAME " + name + " IN ADDRESS BOOK") | |
del address_book[name] | |
print("DELETED NAME " + name + " FROM ADDRESS BOOK") | |
else: | |
print("NAME " + name + " NOT FOUND IN ADDRESS BOOK") | |
menu = """ | |
ADDRESS BOOK APPLICATION | |
Please input one of the following commands (e.g. 'Command') then press enter: | |
'Create' add a new name and email address | |
'Read' look up a person's email address | |
'Update' change an existing email address | |
'Destroy' delete an existing name and email address | |
""" # multiline string http://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string ... | |
menu_choice = input(menu) | |
handle_menu_choice(menu_choice) | |
# | |
# the program should pickle the dictionary and save it to a file when the user exits the program. | |
# | |
print(address_book) | |
print("WRITING TO FILE -- " + ADDRESS_BOOK_FILE_NAME) | |
pickle.dump(address_book, open(ADDRESS_BOOK_FILE_NAME, "wb" )) # https://wiki.python.org/moin/UsingPickle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment