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
Question
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
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Here's a Python program that performs the required operations on a dictionary of friends' names and their phone numbers:
def display_all_friends(friends):
print("\nName\t\tPhone Number")
for name, phone in friends.items():
print(f"{name}\t\t{phone}")
def add_friend(friends, name, phone):
friends[name] = phone
print(f"\nAdded {name} with phone number {phone}.")
display_all_friends(friends)
def delete_friend(friends, name):
if name in friends:
del friends[name]
print(f"\nDeleted {name} from the dictionary.")
else:
print(f"\n{name} not found in the dictionary.")
display_all_friends(friends)
def modify_phone_number(friends, name, new_phone):
if name in friends:
friends[name] = new_phone
print(f"\nUpdated {name}'s phone number to {new_phone}.")
else:
print(f"\n{name} not found in the dictionary.")
display_all_friends(friends)
def check_friend(friends, name):
if name in friends:
print(f"\n{name} is present in the dictionary.")
else:
print(f"\n{name} is not present in the dictionary.")
def display_sorted_friends(friends):
sorted_friends = dict(sorted(friends.items()))
display_all_friends(sorted_friends)
friends = {}
num = int(input("Enter the number of friends: "))
for _ in range(num):
name = input("Enter friend's name: ")
phone = input("Enter friend's phone number: ")
friends[name] = phone
print("\nMenu:")
print("a) Display all friends")
print("b) Add a new friend")
print("c) Delete a friend")
print("d) Modify a friend's phone number")
print("e) Check if a friend is present")
print("f) Display friends in sorted order")
print("q) Quit")
while True:
choice = input("\nEnter your choice: ").lower()
if choice == 'a':
display_all_friends(friends)
elif choice == 'b':
name = input("Enter friend's name: ")
phone = input("Enter friend's phone number: ")
add_friend(friends, name, phone)
elif choice == 'c':
name = input("Enter the name of the friend to delete: ")
delete_friend(friends, name)
elif choice == 'd':
name = input("Enter the name of the friend to modify: ")
new_phone = input(f"Enter the new phone number for {name}: ")
modify_phone_number(friends, name, new_phone)
elif choice == 'e':
name = input("Enter the name of the friend to check: ")
check_friend(friends, name)
elif choice == 'f':
display_sorted_friends(friends)
elif choice == 'q':
break
else:
print("Invalid choice. Please choose a valid option.")
Explanation
Display all friends (a): Prints all key-value pairs in the dictionary.
Add a new friend (b): Adds a new entry to the dictionary.
Delete a friend (c): Removes a specific entry from the dictionary.
Modify a friend's phone number (d): Updates the value for a specific key in the dictionary.
Check if a friend is present (e): Checks if a key exists in the dictionary.
Display friends in sorted order (f): Prints the dictionary sorted by keys (names).
Quit (q): Exits the program.
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month