Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

\f

Answered: 1 week ago