Create a program that display a message in case the key is not found.
def display_message(dictionary, key):
if key in dictionary:
print(f"The value of {key} is {dictionary[key]}")
else:
print(f"The key {key} was not found in the dictionary.")
# Example usage
my_dictionary = {"apple": "a sweet fruit", "banana": "a yellow fruit", "orange": "a citrus fruit"}
display_message(my_dictionary, "apple") # Output: The value of apple is a sweet fruit
display_message(my_dictionary, "pear") # Output: The key pear was not found in the dictionary.
display_message
function takes two parameters: dictionary
and key
. It checks if the key
exists in the dictionary
using the in
operator. If the key
is found, it prints the corresponding value. Otherwise, it prints a message indicating that the key was not found in the dictionary.