Ask Question
21 February, 11:52

Write 3 functions in the starter code below such that: add_to_dict () : takes a dictionary, a key, a value and adds the key, value pair to the dictionary. If key is already in dictionary then it displays the error message: "Error. Key already exists.". Returns dictionary. remove_from_dict () : takes a dictionary and key and removes the key from the dictionary. Returns dictionary. If no such key is found in the dictionary then it prints: "No such key exists in the dictionary.". Hint: Use try-except. find_key (dictt, key) : takes dictionary and key and prints value corresponding to the key from the dictionary: print ("Value: ", value). If key is not found, then prints: "Key not found." Hint: Use try-except

+4
Answers (1)
  1. 21 February, 13:52
    0
    Required code is given below:

    Explanation:

    def add_to_dict (dictt, key, value):

    if key in dictt. keys ():

    print ("Error. Key already exists.")

    else:

    dictt[key]=value

    return dictt

    def remove_from_dict (dictt, key):

    try:

    dictt[key]

    dictt. pop (key, None)

    return dictt

    except KeyError:

    print ("No such key exists in the dictionary.")

    def find_key (dictt, key):

    try:

    value=dictt[key]

    print ("Value: ", value)

    except KeyError:

    print ("Key not found.")
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write 3 functions in the starter code below such that: add_to_dict () : takes a dictionary, a key, a value and adds the key, value pair to ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers