DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

M__
M__

Posted on

DAY 8: DICTIONARIES

Things are starting to become more tough and its taking longer to solve the challenges but the positive side of that is it’s the learning journey. Nothing worth doing is ever easy and the feeling of learning something new that you did not know before is an achievement worth recognizing.

Dictionaries are part of the data structures in Python which store their values in form of key-value pairs.

The task:
Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.

n = int(input())

#create a dictionary variable
d = dict()

#accept input from the user and add the input into the dictionary variable
for i in range(0, n):
    name, number = input().split()
    d[name] = int(number)

#accept input by looping through the input size for names to search for in the dictionary.
for i in range(0, n):
    try:
        name = input()
        if name in d:
            print(f"{name}={d[name]}")
        else:
            print("Not found")
    except:
        break


'''
Sample Input:

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry

Sample Output:

sam=99912222
Not found
harry=12299933

'''
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Find and follow new tags! πŸ€” Did you know? Β  DEV has a variety of tags to help you find the content you like. Find and follow your favorite tags