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

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 05:00
Which two editions of windows 7 support 64 bit cpus? choose two out of professional, business, starter, or home premium.
Answers: 1
question
Computers and Technology, 22.06.2019 21:00
Simon says is a memory game where "simon" outputs a sequence of 10 characters (r, g, b, y) and the user must repeat the sequence. create a for loop that compares the two strings starting from index 0. for each match, add one point to userscore. upon a mismatch, exit the loop using a break statement. assume simonpattern and userpattern are always the same length. ex: the following patterns yield a userscore of 4: simonpattern: rrgbryybgy userpattern: rrgbbrybgy
Answers: 2
question
Computers and Technology, 23.06.2019 01:00
Let r be a robotic arm with a fixed base and seven links. the last joint of r is a prismatic joint, the other ones are revolute joints. give a set of parameters that determines a placement of r. what is the dimension of the configuration space resulting from your choice of parameters?
Answers: 3
question
Computers and Technology, 23.06.2019 16:00
What is the biggest difference between section breaks and regular page breaks? section breaks are more difficult to add than page breaks. section breaks make it easier for you to view the document as an outline. section breaks allow you to have areas of the document with different formatting. section breaks are smaller than regular page breaks.
Answers: 2
You know the right answer?
Reset to Starter Code

Write 3 functions in the starter code below such that:
Questions
question
Mathematics, 19.01.2020 01:31
question
Social Studies, 19.01.2020 01:31
question
Mathematics, 19.01.2020 01:31
Questions on the website: 13722367