English

Write a program to input your friends’ names and Phone Numbers and store them in the dictionary as the key-value pair. Perform the following operations on the dictionary: Display the name - Computer Science (Python)

Advertisements
Advertisements

Question

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

  1. Display the name and phone number of all your friends
  2. Add a new key-value pair in this dictionary and display the modified dictionary
  3. Delete a particular friend from the dictionary
  4. Modify the phone number of an existing friend
  5. Check if a friend is present in the dictionary or not
  6. Display the dictionary in sorted order of names
Answer in Brief

Solution

Program: 
dic = {}
#Creates an empty dictionary

#While loop to provide the options repeatedly
#it will exit when the user enters 7
while True:
    print("1. Add New Contact")
    print("2. Modify Phone Number of Contact")
    print("3. Delete a Friend's contact")
    print("4. Display all entries")
    print("5. Check if a friend is present or not")
    print("6. Display in sorted order of names")
    print("7. Exit")
    inp = int(input("Enter your choice(1-7): "))

    #Adding a contact
    if(inp == 1):
        name = input("Enter your friend name: ")
        phonenumber = input("Enter your friend's contact number: ")
        dic[name] = phonenumber
        print("Contact Added \n\n")
    #Modifying a contact if the entered name is present in the dictionary
    elif(inp == 2):
        name = input("Enter the name of friend whose number is to be modified: ")
        if(name in dic):
            phonenumber = input("Enter the new contact number: ")        
            dic[name] = phonenumber
            print("Contact Modified\n\n")
        else:
            print("This friend's name is not present in the contact list")
    #Deleting a contact if the entered name is present in the dictionary
    elif(inp == 3):
        name = input("Enter the name of friend whose contact is to be deleted: ")
        if(name in dic):
            del dic[name]
            print("Contact Deleted\n\n")
        else:
            print("This friend's name is not present in the contact list")
    #Displaying all entries in the dictionary
    elif(inp == 4):
        print("All entries in the contact")
        for a in dic:
            print(a,"\t\t",dic[a])
        print("\n\n\n")
    #Searching a friend name in the dictionary
    elif(inp == 5):
        name = input("Enter the name of friend to search: ")
        if(name in dic):
            print("The friend",name,"is present in the list\n\n")
        else:
            print("The friend",name,"is not present in the list\n\n")
    #Displaying the dictionary in the sorted order of the names
    elif(inp == 6):
        print("Name\t\t\tContact Number")
        for i in sorted(dic.keys()):
            print(i,"\t\t\t",dic[i])
        print("\n\n")
    #Exit the while loop if user enters 7
    elif(inp == 7):
        break
    #Displaying the invalid choice when any other values are entered
    else:
        print("Invalid Choice. Please try again\n")

OUTPUT:
1. Add New Contact
2. Modify Phone Number of Contact
3. Delete a Friend's contact
4. Display all entries
5. Check if a friend is present or not
6. Display in sorted order of names
7. Exit
Enter your choice(1-7): 1
Enter your friend name: Mohit
Enter your friend's contact number: 98765*****
Contact Added 
 
1. Add New Contact
2. Modify Phone Number of Contact
3. Delete a Friend's contact
4. Display all entries
5. Check if a friend is present or not
6. Display in sorted order of names
7. Exit
Enter your choice(1-7): 1
Enter your friend name: Mohan
Enter your friend's contact number: 98764*****
Contact Added 

1. Add New Contact
2. Modify Phone Number of Contact
3. Delete a Friend's contact
4. Display all entries
5. Check if a friend is present or not
6. Display in sorted order of names
7. Exit
Enter your choice(1-7): 6
Name Contact Number
Mohan   98764*****
Mohit   98765*****
 
1. Add New Contact
2. Modify Phone Number of Contact
3. Delete a Friend's contact
4. Display all entries
5. Check if a friend is present or not
6. Display in sorted order of names
7. Exit
Enter your choice(1-7): 7
shaalaa.com
Dictionary Operations
  Is there an error in this question or solution?
Chapter 10: Tuples and Dictionaries - Exercise [Page 225]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 10 Tuples and Dictionaries
Exercise | Q 5. | Page 225
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×