In the Image
A Python Program
Answers
The following code has been written using Python.
phone_dict = dict()
n = int(input("Enter the number of contacts: "))
print()
for i in range(n):
nm = input("Enter the name: ")
num = int(input("Enter the number: "))
phone_dict[nm] = num
print()
while True:
print("1. Display the phone book")
print("2. Add a new contact")
print("3. Delete a contact")
print("4. Modify a contact's number")
print("5. Check if a contact exists")
print("6. Display a sorted version of the phone book")
print("7. Exit")
print()
o = int(input("Enter your choice: "))
print()
#displaying the dictionary
if o == 1:
print("Name\t", "Phone number")
print()
for i in phone_dict:
print(i, "\t", phone_dict[i])
print()
#adding a new contact and displaying the new dictionary
elif o == 2:
new = input("Enter the name of the new contact: ")
nnum = input("Enter the number of the new contact: ")
phone_dict[new] = nnum
print()
print("Updated phone book:")
print()
print("Name\t", "Phone number")
print()
for i in phone_dict:
print(i, "\t", phone_dict[i])
print()
#deleting a contact
elif o == 3:
dn = input("Enter the name of the contact you'd like to delete: ")
print()
if dn in phone_dict.keys():
del phone_dict[dn]
else:
print("No such contact.")
print()
#modifying a contact's number
elif o == 4:
mn = input("Enter the name of the contact whose number you'd like to modify: ")
print()
if mn in phone_dict.keys():
nmn = input("Enter the new number: ")
phone_dict[mn] = nmn
else:
print("No such contact.")
print()
#searching for a contact
elif o == 5:
sn = input("Enter a name: ")
print()
if sn in phone_dict.keys():
print("The contact exists.")
else:
print("No such contact.")
print()
#displaying the phone book in order of names
elif o == 6:
l = sorted(phone_dict)
print("Name\t", "Phone number")
print()
for i in l:
print(i, "\t", phone_dict[i])
print()
elif o == 7:
break