如何调用位于另一个字典中的字典键



在一个简单的函数上有一个小问题需要解决我想能够编辑一个dictupdate(),但问题是,我想更新它使用它的值,而不是它的键下面是我的代码:

contacts = {"Mohamed": {"name": "Mohamed Sayed", "number": "0123565665", "birthday": "24.11.1990", "address": "Ginnheim 60487"},
"Ahmed": {"name": "Ahmed Sayed", "number": "0123456789", "birthday": "06.06.1990", "address": "India"}}

def edit_contact():
user_input = input("Please enter the name of the contact you want to edit: ")
for k in contacts:
if user_input == contacts["Mohamed"]["name"]:
print(contacts)

如果他们输入名字,那么子字典就是contacts[name]。你不需要遍历整个联系人字典。

def edit_contact():
name = input("Please enter the first name of the contact you want to edit: ")
if name in contacts:
print(contacts[name])
else:
print("I could not find that contact")

相关内容

  • 没有找到相关文章

最新更新