我正在尝试创建一个搜索功能,在该功能中获得用户的input
,然后它在我的config.json
中进行搜索,以查看是否存在具有相同名称的条目,如果是,它将打印该值如果没有,那么pass
recipient_finder = input("Recipient: ")
for receiver in config['email_list']:
if receiver == recipient_finder:
print([recipient_finder])
这是在我的config.json
"email_list": {
"mediamarkt": "contact@mediamarkt.nl",
"lenovo": "consumerts@lenovo.com",
"godaddy": "hq@godaddy.com",
"stackoverflow": "legal@stackoverflow.com"
}
问题是我没有从搜索中收到实际的电子邮件,而是获得了一个密钥(因此我不会得到hq@godaddy.com,而只会得到godaddy)
答案 0 :(得分:0)
您可以在下面尝试此操作
recipient_finder = input("Recipient: ")
for receiver in config['email_list']:
if receiver == recipient_finder:
print(config['email_list'][recipient_finder])
OR
if recipient_finder in config['email_list']:
print(config['email_list'][recipient_finder])
答案 1 :(得分:0)
检查用户输入是否是字典中的键。如果是,则打印相应的值。
if receiver in config['email_list']:
print(config['email_list'][receiver]
else:
print("Not found")