我正在尝试格式化字典键,以便可以要求用户输入值并将其添加到字典中。 到目前为止,我的字典看起来像这样:
{'admin': ' '}
预期输出:
name: admin
admin's password: admin
代码:
new dictionary = {'admin': 'admin'}
def example():
name= input("name: ")
#no value yet for the input key
value = ""
dict_users[name] = value
check(dict_users)
def check(name):
if name in dict_users.items():
password = input('{name}'.format(**dict_users) + "s"+ " Password: "))
#add password input to existing dictionary key
dict_users[name] = password
#Dictionary of Users
dict_users = {}
答案 0 :(得分:2)
无需在此处使用字符串格式。
另外,请查看如何测试字典中是否存在键
def check(name):
if name in dict_users:
password = input(name + "'s"+ " password: "))
#add password input to existing dictionary key
dict_users[name] = password