Ask Question
6 February, 02:02

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

+1
Answers (1)
  1. 6 February, 02:04
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers