#Creates the Animal class.
class Animal:
def __init__(self, name, age, type_of_animal):
self.name = name
self.age = age
self.animal_type = type_of_animal
def bio(self):
#Checks if the animal's type begins with a vowel.
if self.animal_type[0].lower() in "aeiou":
#Checks if the animal is older than 1.
if self.age >= 2:
print(f"{self.name.capitalize()} is an {self.animal_type.capitalize()} and they are {self.age} years old.")
else:
print(f"{self.name.capitalize()} is an {self.animal_type.capitalize()} and they are {self.age} year old.")
#If it isn't a vowel it's a consonant.
else:
if self.age >= 2:
print(f"{self.name.capitalize()} is a {self.animal_type.capitalize()} and they are {self.age} years old.")
else:
print(f"{self.name.capitalize()} is a {self.animal_type.capitalize()} and they are {self.age} year old.")
#Animals
a1 = Animal("milo", 1, "tree frog")
a2 = Animal("diego", 2, "anaconda")
a3 = Animal("paul", 1, "aardvark")
a4 = Animal("pietro", 7, "cheetah")
a5 = Animal("bob", 1, "dog")
#Animal list
animal_list = [a1,a2,a3,a4,a5]
#Adds more animals to the list
animal_list.append(Animal("kally", 5, "horse"))
animal_list.append(Animal("oni", 1, "python"))
animal_list.append(Animal("kat", 4, "ocelot"))
#Applies .bio() method to each individual animal in the list.
for i in range(len(animal_list)):
animal_list[i].bio()
This code defines a system for creating animal objects (such as a frog, anaconda, and ocelot) and provides a way to generate a simple bio for each animal based on its type and age. It's a basic example of object-oriented programming (OOP) in Python, where each animal is an object with attributes like name, age, and species. The goal of the code is to create animals, store them in a list, and then display personalized bios for each animal in a user-friendly format. How can I make this more efficient?