Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have mentioned a python code down below. I want to know if there is another way to solve the program using different logic and

I have mentioned a python code down below. I want to know if there is another way to solve the program using different logic and methods. PLEASE THE CODE SHOULD BE IN PYTHON. I AM LOOKING FOR AN ALTERNATIVE WAY TO DO THE SAME THING BUT WITH DIFFERENT LOGIC AND WAY. A COMPLETE REDESIGN.

BUT USE CLASSES AND INHERITANCE, IMPORT JSON METHOD IS NOT WORKING.

PLEASE NOTE THAT I NEED A WORKING CODE SINCE I HAVE POSTED THIS QUESTION TWICE AND ON THE FIRST QUESTION ANSWER THE CODE WAS NOT WORKING

THS IS THE TXT FILE:

Bob Mammal Bear 300 2 Lucy Reptile Lizard 2 Nonvenomous Carl Reptile Cottonmouth 3 Venomous Oliver Bird Ostrich 75 60 Mute Polly Bird Parrot 1 2 Talks I want a cracker Doug Mammal Dog 20 4

PYTHON CODE:

# Animal class

class Animal:

# Constructor to initialize data members

def __init__(self, name, type, species, mass):

self.name = name

self.type = type

self.species = species

self.mass = mass

# Getter methods for data members

def getName(self):

return self.name

def getSpecies(self):

return self.species

def getType(self):

return self.type

def getMass(self):

return self.mass

# Mammal class inheriting Animal class

class Mammal(Animal):

def __init__(self, name, type, species, mass, litterSize):

Animal.__init__(self, name, type, species, mass)

self.litterSize = litterSize

def getLitterSize(self):

return self.litterSize

# Reptiles class inheriting Animal class

class Reptiles(Animal):

def __init__(self, name, type, species, mass, VenomousOrNot):

Animal.__init__(self, name, type, species, mass)

self.VenomousOrNot = VenomousOrNot

def getVenomousOrNot(self):

return self.VenomousOrNot

# Birds class inheriting Animal class

class Birds(Animal):

def __init__(self, name, type, species, mass, Wingspan, talksOrMute, phrase):

Animal.__init__(self, name, type, species, mass)

self.Wingspan = Wingspan

self.talksOrMute = talksOrMute

self.phrase = phrase

def getWingspan(self):

return self.Wingspan

def getTalksOrMute(self):

return self.talksOrMute

def getPhrase(self):

return self.phrase

if __name__ == '__main__':

# Open and read file

file_read = open('ZOO.txt', 'r')

# List which will contain all animal objects- mammal, reptiles and birds

animalsList = []

# For each line in file

for line in file_read:

# Split each line and store in detail

details = line.split()

# If type is mammal, create mammal object and add to animalsList

if details[1] == "Mammal":

m = Mammal(details[0], details[1], details[2], details[3], details[4])

animalsList.append(m)

# If type is reptiles, create reptiles object and add to animalsList

elif details[1] == "Reptile":

r = Reptiles(details[0], details[1], details[2], details[3], details[4])

animalsList.append(r)

# If type is bird, create bird object and add to animalsList

elif details[1] == "Bird":

phrase = ""

# Check if bird talks, if yes, read next line and set phrase to it

if details[5] == "Talks":

phrase = file_read.readline().rstrip(' ')

b = Birds(details[0], details[1], details[2], details[3], details[4], details[5], phrase)

animalsList.append(b)

# Create a loop which terminates when user enters e

while True:

choice = input('Query animal species[s], mass[m], litter[l], venom[v], wingspan[w], talk[t] or exit session[e]? ')

# Check which choice is entered and ask for animal name

if choice == 's':

name = input('Animal Name? ')

# Search for the name in list

# If exists, print species

# Else, print not present

for animal in animalsList:

if name == animal.getName():

print(name, 'species is', animal.getSpecies())

break

else:

print(name, 'not present')

elif choice == 'm':

name = input('Animal Name? ')

# Search for the name in list

# If exists, print mass

# Else, print not present

for animal in animalsList:

if name == animal.getName():

print(name, 'mass is', animal.getMass())

break

else:

print(name, 'not present')

elif choice == 'l':

name = input('Animal Name? ')

# Search for the name in list

# If exists, nd if it's type is mammal ,print litter size

# Else, print not present

for animal in animalsList:

if name == animal.getName() and animal.getType() == "Mammal":

print(name, 'litter size is', animal.getLitterSize())

break

else:

print(name, 'not present')

elif choice == 'v':

name = input('Animal Name? ')

# Search for the name in list

# If exists, nd if it's type is reptile ,print venomous or not

# Else, print not present

for animal in animalsList:

if name == animal.getName() and animal.getType() == "Reptile":

print(name, 'is', animal.getVenomousOrNot())

break

else:

print(name, 'not present')

elif choice == 'w':

name = input('Animal Name? ')

# Search for the name in list

# If exists, nd if it's type is bird ,print wings span

# Else, print not present

for animal in animalsList:

if name == animal.getName() and animal.getType() == "Bird":

print(name, 'Wing Span is', animal.getWingspan())

break

else:

print(name, 'not present')

elif choice == 't':

name = input('Animal Name? ')

# Search for the name in list

# If exists, nd if it's type is bird ,print talks or mute

# If talks, print phrase as well

# Else, print not present

for animal in animalsList:

if name == animal.getName() and animal.getType() == "Bird":

if animal.getTalksOrMute() == "Mute":

print(name, 'is Mute')

else:

print(name, 'talks.')

print(name, 'says', animal.getPhrase())

break

else:

print(name, 'not present')

elif choice == 'e':

# Terminate loop

print('Goodbye!')

break

else:

print('Invalid choice entered!')

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

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions

Question

Compare Jung and Adlers theories to Freuds psychoanalysis.

Answered: 1 week ago

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago