153k views
3 votes
12.3 code practice question 3 python project stem please help

Instructions

Write a method replace_values(dcn, search, replace) that searches through the dictionary and replaces any value which matches search with replace instead. For example, the following call to the method
d = {"Mel": "Coke", "Eva": "Sprite", "Tao": "Coke", "Em": "Fanta"}
replace_values(d, "Coke", "Pepsi")
should result in the dictionary d becoming
{'Mel': 'Pepsi', 'Eva': 'Sprite', 'Tao': 'Pepsi', 'Em': 'Fanta'}



1 Answer

4 votes

def replace_values(dcn, search, replace):

for key in dcn:

if dcn[key] == search:

dcn[key] = replace

print(dcn)

User Hanaa
by
7.8k points