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")
6 Antworten
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])
0
if contact in contacts:
print(contacts[contact][1])
else:
print("Not found")
0
# simple
try:
print(contacts[input()][1])
except:
print('Contact not Found')
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!
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
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')