Question: Provided is a buggy for loop that tries to accumulate some values out of some dictionaries. Insert a try/except so that the code passes. If
Provided is a buggy for loop that tries to accumulate some values out of some dictionaries. Insert a try/except so that the code passes. If the key is not there, initialize it in the dictionary and set the value to zero.
Save & Run
4/22/2020, 4:20:25 PM - 13 of 13Show in CodeLens
1
2
di = [{"Puppies": 17, 'Kittens': 9, "Birds": 23, 'Fish': 90, "Hamsters": 49}, {"Puppies": 23, "Birds": 29, "Fish": 20, "Mice": 20, "Snakes": 7}, {"Fish": 203, "Hamsters": 93, "Snakes": 25, "Kittens": 89}, {"Birds": 20, "Puppies": 90, "Snakes": 21, "Fish": 10, "Kittens": 67}] 3
total = 0
4
for diction in di:
5
try:
6
total = total + diction['Puppies']
7
except:
8
total= diction
9
10
print("Total number of puppies:", total) 11
12
13
Total number of puppies: {'Puppies': 90, 'Kittens': 67, 'Birds': 20, 'Fish': 10, 'Snakes': 21}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
