Ask Question
26 March, 14:39

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. 26 March, 15:47
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers