Python 查字典

Python 查字典,python,dictionary,Python,Dictionary,我需要编写一个函数get_value(dictionary,key),它返回给定字典中给定键的值 我的代码 dictionary = {'Sunday':0, 'Monday':1, 'Tuesday':2, 'Wednesday':3, 'Thursday':4, 'Friday':5, 'Saturday':6} get_value = input("Provide key") for day in dictionary: if dictionary[day]['key'] =

我需要编写一个函数
get_value(dictionary,key)
,它返回给定字典中给定键的值

我的代码

dictionary = {'Sunday':0, 'Monday':1, 'Tuesday':2, 'Wednesday':3, 'Thursday':4, 'Friday':5, 'Saturday':6}  
get_value = input("Provide key") 
for day in dictionary:
    if dictionary[day]['key'] == get_value:
        print (day)

要访问字典中的值,只需使用
字典[key]
,因此在函数中只需返回此值。

尝试以下操作:

dictionary = {'Sunday': 0, 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5, 'Saturday': 6}  

get_value = input("Provide key: ") 

if get_value in dictionary:
    print(dictionary[get_value])
else:
    print('Key not found!')

经核准的。您可以继续。我在您的代码中没有看到任何
get\u value
函数,您尝试了什么?
get\u value=input(“提供键”)
print(dictionary.get(get\u value,'key not found!”)