0

Dictionary Functions Python

You are making a phonebook. The contacts are stored in a dictionary, where the key is the name and the value is a list, representing the number and the email of the contact. You need to implement search: take the name of the contact as input and output the email. If the contact is not found, output "Not found". Note, that the email is the second element of the list. contacts = { "David": ["123-321-88", "david@test.com"], "James": ["241-879-093", "james@test.com"], "Bob": ["987-004-322", "bob@test.com"], "Amy": ["340-999-213", "a@test.com"] } #your code goes here contact = input() if contact in contacts: print(contact) else: print("Not found")

14th Jul 2021, 4:07 AM
Ashwin Jain
Ashwin Jain - avatar
6 odpowiedzi
0
you code only print the input provided... but you must print the related email instead ^^ using .get() method of dict, you could do it in one line: print(contacts.get(contact,["","Not found"])[1])
14th Jul 2021, 4:21 AM
visph
visph - avatar
0
if contact in contacts: print(contacts[contact][1]) else: print("Not found")
14th Jul 2021, 5:35 AM
Shadoff
Shadoff - avatar
0
# simple try: print(contacts[input()][1]) except: print('Contact not Found')
14th Jul 2021, 6:22 AM
Ervis Meta
Ervis Meta - avatar
0
Just to push your knowledge a little bit further: try to use a named tuple instead of a list. That way you don't need to use an index but can use an attribute. It's much easier to read that way :-) You will find indepth infos here: https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-namedtuple/ Have a nice day!
14th Jul 2021, 8:05 AM
Zen Coding
Zen Coding - avatar
0
inputName = input("") if inputName in contacts: print(contacts[inputName][1]) elif inputName not in contacts: print('Not found') Try this and follow me on instagram @aniket_sng
15th Oct 2021, 3:14 PM
ANIKET SINGH
ANIKET SINGH - avatar
0
try this, contacts = { "David": ["123-321-88", "david@test.com"], "James": ["241-879-093", "james@test.com"], "Bob": ["987-004-322", "bob@test.com"], "Amy": ["340-999-213", "a@test.com"] } #your code goes here inputName = input("") if inputName in contacts: print(contacts[inputName][1]) elif inputName not in contacts: print('Not found')
31st May 2022, 12:38 PM
sudesh wimalaweera