Question
THIS IS A PYTHON CODE. I WANT TO KNOW IF IS THERE ANOTHER WAY TO SOLVE THIS. I MEAN TO SAY WITH DIFFERENT LOGIC AND
THIS IS A PYTHON CODE. I WANT TO KNOW IF IS THERE ANOTHER WAY TO SOLVE THIS. I MEAN TO SAY WITH DIFFERENT LOGIC AND ALTERNATIVE WAY. A COMPLETE REDESIGN. I HAVE MADE CLASSES, SETTERS, AND GETTERS ASWELL BUT I WANT CHANGES IN THE MAIN() FUNCTION.
THE CODE WILL READ THIS 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 :
def main():
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 = Reptile(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 = Bird(details[0], details[1], details[2], details[3], details[4], details[5], phrase)
animalsList.append(b)
while True:
choice = input('Query animal species[s], mass[m], litter[l], venom[v], wingspan[w], talk[t] or exit session[e]? ')
if choice == 's':
name = input('Animal Name? ')
for animal in animalsList:
if name == animal.name:
print(name, 'species is', animal.species)
break
else:
print(name, 'is not present')
elif choice == 'm':
name = input('Animal Name? ')
for animal in animalsList:
if name == animal.name:
print(name, 'mass is', animal.mass)
break
else:
print(name, 'is not present')
elif choice == 'l':
name = input('Animal Name? ')
for animal in animalsList:
if isinstance(animal, Mammal) and name == animal.name:
print(name, 'litter size is', animal.litter_size)
break
else:
print(name, 'is not present')
elif choice == 'v':
name = input('Animal Name? ')
for animal in animalsList:
if isinstance(animal, Reptile) and name == animal.name:
print(name, 'venomous status is', animal.venomous)
break
else:
print(name, 'is not present')
elif choice == 'w':
name = input('Animal Name? ')
for animal in animalsList:
if isinstance(animal, Bird) and name == animal.name:
print(name, 'wingspan is', animal.wingspan)
break
else:
print(name, 'is not present')
elif choice == 't':
name = input('Animal Name? ')
for animal in animalsList:
if isinstance(animal, Bird) and name == animal.name:
if animal.talks:
print(name, 'says', animal.phrase)
else:
print(name, 'does not talk')
break
else:
print(name, 'is not present')
elif choice == 'e':
break
if __name__ == '__main__':
main()
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