Write a Python program to input ‘n’names and phone numbers to store it in a dictionary and to search and print the phone number of that particular name.
Answers
Answer:
Explanation:
phonebook = dict( )
n = input (“Enter total number of friends”)
i = 1 while i<=n : a = raw_input (“Enter name”) b = raw_input (“Enter phone number”)
phonebook [a] = b i = i + 1 name = raw_input (“Enter name”) f = 0 l = phonebook.keys( ) for i in l : if (comp (i, name) = = 0) : print “Phone number =”, phonebook[i] f = 1 if (f = = 0): print “Given name not exist”
The program is given below:
phonebook_directory = dict( )
n = input (” Enter number of friends you want to add with their phone numbers”) // enter the name
A = x
while A < = n : // while loop
y = raw_input (“enter name”) // name is given as input
z = raw_input (“enter phone number”) // phone number is given as input
phonebook_directory [y] = z
A = A + x
name = raw_input (“enter the name to be searched ”) // searching of name
g = 0
K= phonebook_directory . keys( ) // keys are checked
for A in x :
if (cmp(A,name) == 0):
print ( “Phone number= “ , phonebook_directory[A] ) // output is printed
g = x
if (g==0):
print ( “Unknown name” ) //when name does not match with the stored names