Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

rite a Python program eeps a person s name an eir cont rmation ema resses and ph

ID: 644751 • Letter: R

Question

rite a Python program eeps a person s name an eir cont rmation ema resses and phone number n an address book. You address book will be a dictionary of key-value pairs. The program should display a menu to let a user: ook up a person's email address ook up a person's phone number add a new person with an email address and a phone number update a person's existing email address update a person's existing phone number update a person's last name (say a person got married) delete a person and existing contact information print all persons with their email and phone number delete all data from phone book exit program Your program should pickle the dictionary and save it to a pickled file when user exits the program. Each time your program starts; it should retrieve the file and un-pickle the dictionary from the file. Use "emails dat as pickle file name Hints Everything should be saved as string you may need to use other primitive types for checking if input is valid You should verify all input for correctness and correct format at this point in the semester you should be doing this ithout being told to do so explicitly For phone numbers, assume North American Numbering Plan (US and Canada), that is XXX-XXX-XXXX (10 digits and o The user should a enter phone number in format: XXX-XXX-XXXX no other format should be accepted The user should enter 10 digits and DASHES in-between. For email addresses, assume local-part edomain.top level domain o local-part is the user name (can be any length and combination of letters a to z and/or A to Z and/or digits 0-9) such as google, hotmail, yahoo etc. (can be any length and combination of letters a to z an domain Or A to Z and/or digits 0-9) o top level domain DOT 2 or 3 letters such as .com or net or .us or .edu o The user should a phone number in format: local-part@domain .top level no other format should be accepted Things to consider for this assignment: REMEMBER: every key is unique and can exist only once in dictionary o is first name unique enough? Can 2 people have the same first name? o Is last name unique enough? Can 2 people have the same last name? o Is "last name +first name" or "first name last name" concatenation unique enough? Can 2 people have the same "last name first name" or "first name last name" combinations? o Assuming a phonebook of people, which is the one thing unique enough that you can use as a key? o Each key has to refer to multiple data We covered "some mysterious data structure last week Monday that can hold multiple data of the same type the name begins with L T Can you use tkey: some mysterious data structure

Explanation / Answer

Answer:

#! /usr/bin/env python3
# family1.py
# First I created an empty dictionary.
family= { }
# Next, I added some key:value pairs to the dictionary.
# Note that the value in the key:value pair is a list.
# The list includes a telephone number and an email address.
family['John'] = ['345-5647','john@jmail.com']
family['Sarah'] = ['345-8912', 'sarah@jmail.com']
family['Jill'] = ['345-1212', 'jill@jmail.com']
family['Tim'] = ['345-2319','tim@jmail.com']
# Next I looped through the dictionary and printed out key:value
# pairs. The value is a list. All the elements in the list are
# displayed on the screen.
for key, value in family.items():
print(key,":", value)
print()
# Again, I looped through the dictionary and printed the key
# and a value from the list. In this example, it is the
# telephone number. from the list that isndisplayed on the screen.
for key, value in family.items():
print(key,":", value[0])
print()
# Here, I looped through the dictionary and printed out the key
# and a value from the list. In this example, it is the email
# address from the list.
for key, value in family.items():
print(key,":", value[1])
print()
# Next, from a key:value pair I selected a key and accessed one of
# the elements in the list associated with the key. This will come
# in handly later.
name = "Jill"
telephone = family['Jill'][0]
print(name,":", telephone)
print()
# I did the same for the email address, but used the name variable
# rather than "Jill."
email = family[name][1]
print(name, ":", email)
print()
# Finally, here is another approach using the get() method to
# retrieve the telephone number and then the email address.
print(name,":",family.get(name)[0])
print(name,":",family.get(name)[1])