在一个简单的函数上有一个小问题需要解决我想能够编辑一个dict
与update()
,但问题是,我想更新它使用它的值,而不是它的键下面是我的代码:
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")