Saturday, November 13, 2021

Write a program to input your friends’ names and their Phone Numbers and store them in the dictionary as the key-value pair

 

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


dic = { }
while True :
    name = input( "Enter name of friend :-")
    phone = int( input("Enter phone number of friend :-" ))
    dic [name] = phone
    choise = input("Enter Q to quit otherwise N :-")
    if choise == "Q" or choise == "q" :
        break

#a
print()
print("a :-")
print(dic)

#b
print()
print("b :-")
name = input( "Enter name of friend :-")
phone = int(input("Enter phone number of friend :-" ))
dic [name] = phone
print(dic)

#c
print()
print("c :-")
name = input( "Enter name of that friend which you want to delete :-")
del dic[ name ]
print(dic)

#d
print()
print("d :-")
name = input( "Enter name of that friend which you want to change his phone number :-")
phone = int(input ("Enter phone number of friend :-" ))
del dic [ name]
dic[ name ] = phone
print(dic)

#e
print()
print("e :-")
name = input( "Enter name of that friend which you want to search :-")
if name in dic :
    print(" Found")
else :
    print( "Not Found ")

#f
print()
print("f :-")
newdic = {}
key = list( dic.keys())
key.sort()
for i in key :
    newdic [ i ] = dic [ i ]
print(newdic)



No comments:

Post a Comment