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,](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4debacd4f4_83466f4deba464bc.jpg)
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
Get step-by-step solutions from verified subject matter experts
