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

Hello, I have a problem in my Python coding: Write an address book program that

ID: 3718854 • Letter: H

Question

Hello, I have a problem in my Python coding:

Write an address book program that stores your contacts' names and their
email addresses.

The names and email addresses are originally stored in a file called
phonebook.in, in the format:

Harry Potter
theboywholived@hogwarts.edu
Hermione Granger
brightestwitch@hogwarts.edu
Ron Weasley
roonilwazlib@hogwarts.edu
Draco Malfoy
myfatherwillhearaboutthis@hogwarts.edu
Severus Snape
halfbloodprince@hogwarts.edu
Albus Dumbledore
alasearwax@hogwarts.edu

Your program should read from the file, storing the names and corresponding
email addresses in a dictionary as key-value pairs. Then, the program
should display a menu that lets the user enter the numbers 1 through
5, each corresponding to a different menu item:

1) look up an email address
2) add a new name and email address
3) change an email address
4) delete a name and email address
5) save address book and exit

When the user enters 1, the program should prompt them for a name, and then
print the corresponding email address. If there is no dictionary entry under
that name, the program should print, "Sorry, no contact exists under that name."

When the user enters 2, the program should prompt them for a name and an email
address, then add a new key-value pair to the dictionary.

When the user enters 3, the program should prompt them for a name and a new
email address for that contact. It should change the value of the
corresponding dictionary entry to match the new email address.

When the user enters 4, the program should prompt them for a name and delete
the corresponding dictionary entry.

When the user enters 5, the program should write the names and email addresses
in alphabetical order by first name to the file phonebook.out.

Any help will be appreciated.

Explanation / Answer

contactInfo={}
fo=open("contactInfo.txt")
uname=fo.readline()
mailId=fo.readline()
while mailId:
contactInfo[uname.strip(' ')]=mailId.strip(' ')
print(contactInfo)
uname=fo.readline()
mailId=fo.readline()
fo.close()
print ("(1). look up an 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). save address book and exit")

while 1:
choice=int(input("Enter your choice : "))
if choice==1:
uname=input("Enter name : ")
print(contactInfo.get(uname,"Sorry no cantact exit under the name"))
if choice==2:
uname=input("Enter Name : ")
mailId=input("Enter mail ID : ")
contactInfo[uname]=mailId
if choice==3:
uname=input("Enter name for updating mail ID : ")
if(contactInfo.has_key(uname)):
mailId=input("Enter mail ID : ")
conactInfo[uname]=mailId
if choice==4:
uname=input("Enter name to Delete Entry : ")
if(uname in contactInfo):
del contactInfo[uname]
if choice==5:
fo=open("phonebook.out","w")
keylist=contactInfo.keys()
sorted(keylist)
for key in keylist:
fo.write(key)
fo.write(" ")
fo.write(contactInfo[key])
fo.write(" ")
fo.close()
break
  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote