Question: 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
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 persons email address, add a new name and email address, change an existing email address, and delete an exist- ing 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.
I'm using this code I got from here, but after I put the choice and name, it says "email is not found." For choice, I put "1", and for name I put "xyz"
import pickle import sys
try: f=open('email.dat','rb') d=pickle.load(f) f.close() except: d={}
while True:
print(' 1. Find a email address') print('2. Add name and email address') print('3. Change an email address') print('4. Delete an email address') print('5. Exit ')
choice=input(' Enter a choice: ')
if choice: choice=int(choice) else: print(' Enter a number') continue
if choice == 1:
while True:
name=input(' Enter the name ')
if name: if name in d: print(' %s is the email id of %s ' % (d[name],name)) break else: print(' Email not found ') break else: print(' Name cannot be empty ') continue elif choice==2:
while True: name=input(' Enter the name ')
if name: break; else: print(' Name cannot be empty ') continue
while True: email=input(' Enter the email address ')
if email: d[name]=email break else: print(' Email cannot be empty ') continue elif choice==3:
while True: name=input(' Enter the name to change the email address ')
if name: if name in d: email=input(' Enter the new email address ') d[name]=email print(' Email address changed ') break; else: print(' Name not found ') break else: print(' Name cannot be empty ') continue elif choice == 4:
while True: name=input(' Enter the name to remove ')
if name: if name in d: del d[name] print(' Name and Email address removed ') break; else: print(' Name not found ') break else: print(' Name cannot be empty ') continue
elif choice == 5: f=open('email.dat','wb') pickle.dump(d,f) f.close() sys.exit() else: print(' Enter a valid choice ')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
