Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!