Question: Exercise 2 [Safe Add] Write a function called safeAdd(d, k, v, unsafe=False) that takes three or four input parameters when called. Here, d is a

 Exercise 2 [Safe Add] Write a function called safeAdd(d, k, v,

Exercise 2 [Safe Add] Write a function called safeAdd(d, k, v, unsafe=False) that takes three or four input parameters when called. Here, d is a dictionary, k is a key and V is a value. When only three inputs are given (or the fourth is False) the function will add the key:value pair k:v to the dictionary d only if the key k is NOT in the dictionary. If the key is in the dictionary, the function will display the current value for that key, the new value you passed into the function and ask the user if they wish to proceed and overwrite the existing value. If the optional fourth parameter is True, then just add the key value pair without checking if the key is already present. For example, (user input in yellow highlight) >>> d = ('A':1) >>> safeAdd(d, 'B', 3) >>> display (d) A -> 1 B -> 3 >>> safeAdd(d, 'B', 2) The key B is already in the dictionary. Current value is 3. Do you want to replace this value with 2? (yeso) yes >>> display(a) A -> 1 B -> 2 >>> safeAdd(d, 'A', 12) The key A is already in the dictionary. Current value is 1. Do you want to replace this value with 12? (yeso) no >>> display (d) A -> 1 B -> 2 >>> safeAdd(d, 'A', 22, unsafe=True) >>> d['A' 22 Do NOT use try/except for this

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!