In this challenge, you must create a phone book with names as the keys and phone numbers as the values. The dictionary will first be filled with n key-value pairs like sam 99912222 or tom 11122222. Then the user will search for names like ‘sam’ and it should return back sam=99912222. If a name is entered that is not a key in the dictionary, print ‘Not found’.
Sample Input:
3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
Sample Output:
sam=99912222
Not found
harry=12299933
While an empty list can be created using brackets [], empty dictionary can be created using curly brackets {}. In the code below I read in how many entries/key-value pairs will be added to my dictionary as n. I then create the empty dictionary and call it phone_dict
n = int(input()) #number of key-value pairs
phone_dict = {} #creating an empty dictionary
In order to populate a dictionary (e.g. aDict = {}), you will add the key-value pair using the format: aDict[key] = value. In this case, I’d want to add an entry to my phone_dict as phone_dict[name] = phoneNumber.
I’ll loop over the number of key-value pairs that will be entered. In each loop, I read in a key-value pair, and add it to my dictionary.
n = int(input()) #number of key-value pairs
phone_dict = {} #creating an empty dictionary
for i in range(n):
user_input = input().split() #name and number e.g. ['sara', '555-5555']
phone_dict[user_input[0]] = user_input[1] #aDict[key] = value --> phone_dict[name] = phoneNumber
print(phone_dict)
{'sam': '99912222', 'tom': '11122222', 'harry': '12299933'}
Another more “pythonic” way to fill the dictionary is to use the dict() function.
phone_dict = dict(input().split() for _ in range(n))
Now the user can search for any name and if it’s in the dictionary as a key, it will return the name and phone number. In order to allow an unspecified number of names to be entered, you can use the while True loop. In Visual Studio Code, you can use control+z to exit this type of loop.
To search for a key in a dictionary you can use dictionary.get(key). This will return the key’s value or nothing if the key is not found. There is also an optional argument to return something if the specified key does not exist. In this challenge, we want to return ‘Not found’ if the name they enter is not a key.
while True: #while the user keeps entering names
try:
name = input() #take the user input
if phone_dict.get(name, 'Not found') == 'Not found': #if the name is not in the dictionary
print('Not found')
else:
print("{0}={1}".format(name, phone_dict[name]))
except:
break
Instead of dictionary.get(key, optional) you can also use if key in dictionary to return True/False.
while True: #while the user keeps entering names
try:
name = input() #take the user input
if name in phone_dict:
print("{0}={1}".format(name, phone_dict[name]))
else:
print('Not found')
except:
break
Sample Solution 1:
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input()) #number of key-value pairs
phone_dict = {}
for i in range(n):
user_input = input().split() #name and number e.g. ['sara', '555-5555']
phone_dict[user_input[0]] = user_input[1] #aDict = {} aDict[key] = value
while True:
try:
name = input() #input a key to find
if phone_dict.get(name, 'Not found') == 'Not found':
print('Not found')
else:
print("{0}={1}".format(name, phone_dict[name]))
except:
break
Sample Solution 2:
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input()) #number of key-value pairs
phone_dict = dict(input().split() for _ in range(n))
while True: #while the user keeps entering names
try:
name = input() #take the user input
if name in phone_dict:
print("{0}={1}".format(name, phone_dict[name]))
else:
print('Not found')
except:
break