subject

Reset to Starter Code

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

Example:

Menu:

add(a), remove(r), find(f): a

Key: rich

Value: 1

More (y/n)? y
Menu:

add(a), remove(r), find(f): a

Key: alireza

Value: 2

More (y/n)? n
[('alireza', '2'), ('rich', '1')]

Example 2:

Menu:

add(a), remove(r), find(f): a

Key: pranshu

Value: 1

More (y/n)? y
Menu:

add(a), remove(r), find(f): r

key to remove: enbody
No such key exists in the dictionary.

More (y/n)? n

code

#add_to_dict() goes here:

#remove_from_dict() goes here

#find_key() goes here

def main():
more = True
dictt = {}
dictlst = []
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("Invalid choice.")

do_more = input("More (y/n)? ")
if do_more. lower() != 'y':
more = False
if dictt:
for key, value in dictt. items():
temp = (key, value)
dictlst. append(temp)
print(sorted(dictlst))
main()

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 19:20, mayaparness
Write a program that prompts the user to input a string. the program then uses the function substr to remove all the vowels from the string. for example, if str = "there", then after removing all the vowels, str = "thr". after removing all the vowels, output the string. your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel.
Answers: 2
image
Computers and Technology, 23.06.2019 06:30, jayjay5246
Which option correctly describes a dbms application? a. software used to manage databases b. software used to organize files and folders c. software used to develop specialized images d. software used to create effective presentations
Answers: 1
image
Computers and Technology, 23.06.2019 09:30, ranmmm
The place where the extended axis of the earth would touch the celestial sphere is called the celestial
Answers: 1
image
Computers and Technology, 23.06.2019 21:30, jayybrain6337
Enzo’s balance sheet for the month of july is shown. enzo’s balance sheet (july 2013) assets liabilities cash $600 credit card $4,000 investments $500 student loan $2,500 house $120,000 mortgage $80,000 car $6,000 car loan $2,000 total $127,100 total $88,500 which expression finds enzo’s net worth?
Answers: 1
You know the right answer?
Reset to Starter Code

Write 3 functions in the starter code below such that:

Questions in other subjects:

Konu
English, 22.02.2021 04:10
Konu
Biology, 22.02.2021 04:10
Konu
Biology, 22.02.2021 04:10