問題:
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