Write in Python Write a program that keeps names and email addresses in a dictio
ID: 3701742 • Letter: W
Question
Write in Python
Write a program that keeps names and email addresses in a dictionary as key-value pairs.
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.Explanation / Answer
import pickle
file=open("filename.txt",'r+') # filename.txt is the file in which the data is stored
database= pickle.load(file) # loading data into dictionary database
while(1):
print(' 1.Search e-mail address 2.Add new entry 3.Change e-mail address 4.Delete from database 5.Exit ')
choice=input("Enter your choice ")
if choice==5:
break
if choice==1: # Returns email address of the person if found otherwise returns error message
name=raw_input("Enter the name of the person ")
print("Name : "+name)
if name in database:
print("E-mail : "+database[name])
else:
print("Person not found")
elif choice==2: # Adds new entry into the database
name=raw_input("Enter the name of the person ")
email=raw_input("Enter the e-mail address of the person ")
database[name]=email
print("Entry added to database")
elif choice==3: # Overwrites the original email address of the person if found. Otherwise displays error message
name=raw_input("Enter the name of the person ")
if name in database:
email=raw_input("Enter the new e-mail address of the person ")
database[name]=email
print("E-mail address successfully changed")
else:
print("Person not found")
elif choice==4: # Deletes entry of the person if found otherwise returns error message
name=raw_input("Enter the name of the person ")
if name in database:
del database[name]
print("Entry deleted from database")
else:
print("Person not found")
else:
print('Enter valid choice')
pickle.dump(database,file) # Stores the changed database in the open file object
file.close() # Closes the file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.