经过字典中的值获取键 - Get key by value in dictionary

问题:

I made a function which will look up ages in a Dictionary and show the matching name: 我作了一个函数,能够在Dictionary查询年龄并显示匹配的名称: ide

dictionary = {'george' : 16, 'amber' : 19}
search_age = raw_input("Provide age")
for age in dictionary.values():
    if age == search_age:
        name = dictionary[age]
        print name

I know how to compare and find the age I just don't know how to show the name of the person. 我知道如何比较和查找年龄,但我不知道如何显示此人的名字。 Additionally, I am getting a KeyError because of line 5. I know it's not correct but I can't figure out how to make it search backwards. 另外,因为第5行,我获得了KeyError 。我知道这是不正确的,但我不知道如何使它向后搜索。 函数


解决方案:

参考一: https://stackoom.com/question/XfEA/经过字典中的值获取键
参考二: https://oldbug.net/q/XfEA/Get-key-by-value-in-dictionary
相关文章
相关标签/搜索