- 1
Dictionary functions
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". 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"] } Note, that the email is the second element of the list. please provide the solution, im not getting the logic.
7 ответов
+ 8
If we return "Not found" as a list item then we can grab it with -1 index:
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
name=input()
print(contacts.get(name,["Not found"])[-1])
+ 2
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
+ 1
result = contacts.get(input(), "Not found")
if result != "Not found":
result = result[1]
print(result)
I'm sure there is a better implementation, but here is one way.. ^
+ 1
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
name = input()
x = contacts.get(name, "Not found")
if x == "Not found":
print(x)
else:
print(x[1])
+ 1
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
name=input()
print(contacts.get(name,["Not found"])[-1])
0
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"]
}
inputName = input("Enter the name to find email to it: ")
if inputName in contacts:
print(contacts[inputName][1])
elif inputName not in contacts:
print('Not found')
0
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"]
}
user = input()
if user in contacts :
print(contacts[user][1])
else:
print("Not found")