Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write 3 functions in the starter code below such that . add to-dictl): takes a d

ID: 3903580 • Letter: W

Question

Write 3 functions in the starter code below such that . add to-dictl): 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 eror message: 'Error. Key already exists. ? 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 . find_keyldictt, key): takes dictionary and key and prints value corresponding to the key from the dictionary: printlValue:", value). If key is not found, then prints: Key not found. Hint: Use Retums dictionary dictionary.. Hint: Use try-except try-except Example: Menu: add(a), remove), findl: a Key: rich Value: I More (y/n)? y add(a), removefr), find(f]: a Key: alireza Value: 2 More (y/n)? n [[alireza, 2), [rich, T]l Example 2 Menu: add(a], removerl, findlrl: a Key: pranshu Value: 1 More [v/n)? y Menu add(a), removefr), find(f: r key to remove: enbody No such key exists in the dictionary More [v/n)? n

Explanation / Answer

#add_to_dict

def add_to_dict(d,k,v):

if k in d.keys():

print("Key already exists")

else:

d[k]=v

return d

#remove_from_dict

def remove_from_dict(d,k):

if k in d.keys():

del d[k]

return d

else:

print("No key found")

#find_key

def find_key(d,k):

if k in d.keys():

print("value is:",d[k])

else:

print("No key found")

def main():

more=True

dictt={}

dictlist=[]

while more:

print("Menu: ")

choice=input("add(a),remove(r),find(f):")

if choice.lower()=="a":

key=input("key:")

value=input("value:")

dictt=add_to_dict(dictt,key,value)

elif choice.lower()=='r':

key=input("key to remove:")

dictt=remove_from_dict(dictt,key)

elif choice.lower()=='f':

key=input("key to locate:")

find_key(dictt,key)

else:

print("Ivalid choice")

if(input("More(y/n):").lower()!='y'):

more=False

if dictt:

for key,value in dictt.items():

temp=(key,value)

dictlist.append(temp)

print(sorted(dictlist))

main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote