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.


Was this helpful?

India's 1st AI Doubt Solver for CBSE, JEE, and NEET

Ask a Question for Free

and then it's just ₹212 a month

Learn how Chatterbot AI can help you succeed

Cover Image of the article Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

This ultimate guide for CBSE Computer Science class 11 has detailed notes, NCERT solutions, cheat sheets, and our free AI-powered doubt-solving assistant, Chatterbot AI.

Cover Image of the article JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 on May 26! Get exam schedule, syllabus, prep tips & more in this guide. Ace India's top engineering test with topper strategies.

Cover Image of the article How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

Ace NEET with expert strategies: Discover effective exam strategies, time management, core concepts mastery, problem-solving techniques, revision tips, and AI-assisted doubt clearing with Chatterbot AI.

Cover Image of the article How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

Ace IIT JEE in 6 months without coaching. Discover expert self-study strategies for Physics, Chemistry, and Math. Master time management, mock tests, and leverage AI tools like Chatterbot AI for personalized doubt-solving.