Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started