88.2k views
0 votes
Write a program to input your friends’ names and their Phone Numbers and store them in the dictionary as the key-value pair. Perform the following operations on the dictionary:

a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names

1 Answer

4 votes

Final answer:

To perform the given operations on a dictionary of friends' names and phone numbers, you can write a program in Python.

The program will allow you to display the name and phone number of all friends, add new friends, delete existing friends, modify phone numbers, check if a friend is present, and display the dictionary in sorted order of names.

Step-by-step explanation:

To write a program that performs the given operations on a dictionary of friends' names and phone numbers, you can use a programming language such as Python.

Here is an example program that accomplishes the task:

friend_dictionary = {}

# a) Display the name and phone number of all your friends
for friend, phone_number in friend_dictionary.items():
print(friend, phone_number)

# b) Add a new key-value pair in this dictionary and display the modified dictionary
new_friend = input('Enter the name of a new friend: ')
new_phone_number = input('Enter the phone number of the new friend: ')
friend_dictionary[new_friend] = new_phone_number
print(friend_dictionary)

# c) Delete a particular friend from the dictionary
delete_friend = input('Enter the name of the friend to delete: ')
del friend_dictionary[delete_friend]

# d) Modify the phone number of an existing friend
existing_friend = input('Enter the name of the friend whose phone number you want to modify: ')
new_phone_number = input('Enter the new phone number: ')
friend_dictionary[existing_friend] = new_phone_number

# e) Check if a friend is present in the dictionary or not
check_friend = input('Enter the name of the friend to check: ')
if check_friend in friend_dictionary:
print('Friend found.')
else:
print('Friend not found.')

# f) Display the dictionary in sorted order of names
for friend in sorted(friend_dictionary):
print(friend, friend_dictionary[friend])
User Conradj
by
7.9k points