Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON 3 The code includes a lot of repetitive if statements. This means that whenever a new animal is added, we will have to update

PYTHON 3

The code includes a lot of repetitive if statements. This means that whenever a new animal is added, we will have to update all these if statements, which in real code, could be spread out across our entire application. This could be annoying and could lead to us forgetting one, which would lead to a hard to find bug. Clean up this code by creating an Animal.py file with an Animal abstract base class (https://www.geeksforgeeks.org/abstract-classes-in-python/).with instance fields for the name and color and abstract methods to get the greeting, food preference, and appearance strings. Next, create a Cat.py file with a Cat class which inherits from the Animal class and implements the 3 abstract methods. Do the same for Dog and Penguin. Next, rewrite the main.py animals list to use an array of the concrete class instances instead of dictionaries and the for loop to use the class methods instead of if statements. Finally, add a new animal type (new file and class), and create a new animal of that type in the animals list. The output of this script should remain the same as the prompt's output, plus the new animal's output.

Code-

main.py

animals = [

{

'name': 'Piero',

'type': 'cat',

'color': 'orange',

},

{

'name': 'Robin',

'type': 'dog',

'color': 'blonde'

},

{

'name': 'Pete',

'type': 'penguin',

'color': 'black and white',

},

{

'name': 'Shadowspawn',

'type': 'cat',

'color': 'black'

},

]

for animal in animals:

print('Greeting:')

if animal['type'] == 'cat':

print(f"{animal['name']} says meow")

if animal['type'] == 'dog':

print(f"{animal['name']} says woof")

if animal['type'] == 'penguin':

print(f"{animal['name']} says squawk")

print(' Food Preference: ')

if animal['type'] == 'cat':

print(f"{animal['name']} wants chicken")

if animal['type'] == 'dog':

print(f"{animal['name']} wants steak")

if animal['type'] == 'penguin':

print(f"{animal['name']} wants fish")

print(' Appearance:')

if animal['type'] == 'cat':

print(f"{animal['name']} has {animal['color']} fur")

if animal['type'] == 'dog':

print(f"{animal['name']} has {animal['color']} fur")

if animal['type'] == 'penguin':

print(f"{animal['name']} has {animal['color']} feathers")

print()

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

What is goodwill, and how is it accounted for on the balance sheet?

Answered: 1 week ago

Question

Show the properties and structure of allotropes of carbon.

Answered: 1 week ago

Question

LO1 Understand risk management and identify its components.

Answered: 1 week ago