Challenge: Animal Class
Description: Create a class in Python 3 named Animal that has specified attributes and methods.
Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3.
Requirements:
Write a class in Python 3 named Animal that has the following attributes and methods and is saved in the file Animal.py.
Attributes
__animal_type is a hidden attribute used to indicate the animal’s type. For example: gecko, walrus, tiger, etc.
__name is a hidden attribute used to indicate the animal’s name.
__mood is a hidden attribute used to indicate the animal’s mood. For example: happy, hungry, or sleepy.
Methods
__init__ is to define the three attributes above and assign their default values.
The value of __mood is to be set randomly. Generate a random number between 1 and 3. Then:
If the number is 1, the __mood attribute is to be set to a value
of happy.
If the number is 2, the __mood attribute is to be set to a value of
hungry.
If the number is 3, the __mood attribute is to be set to a value of
sleepy.
get_animal_type should return the value of the __animal_type field.
get_name should return the value of the __name field.
check_mood should return the value of the __mood field.
Animal Generator Program
Once you have created the Animal class, create another Python file called animalGenerator.py. This program is to use Animal.py as a module.
In animalGenerator.py, prompt the user to enter the name and type of an animal. Create a new Animal object instance to store this data. Then, ask the user if they would like to repeat the process. They should be able to create as many Animal object instances as they would like.
After the user is done creating animals, the program is to use the object’s accessor methods (get_animal_type, get_name, and check_mood) to retrieve the name, type, and mood of each animal. This information is to be formatted and displayed as shown in the sample program output below.
Welcome to the animal generator!
This program creates Animal objects.
What type of animal would you like to create? Gecko
What is the animal’s name? Gordon
Would you like to add more animals (y/n)? y
What type of animal would you like to create? Walrus
What is the animal’s name? Wally
Would you like to add more animals (y/n)? y
What type of animal would you like to create? Tiger
What is the animal’s name? Truman
Would you like to add more animals (y/n)? n
Animal List:
Gordon the Gecko is hungry
Wally the Walrus is sleepy
Truman the Tiger is hungry
SOLUTION-
I have solve the problem in python code with comments and
screenshot for easy understanding :)
CODE-
Animal.py
Python version : 3.6
Python program to create class Animal
'''
class Animal:
# constructor to initialize the values of animal type,
name and mood
def __init__(self, animal_type , name):
self.__animal_type =
animal_type
self.__name = name
value = random.randint(1,3)
if value == 1:
self.__mood =
"happy"
elif value == 2:
self.__mood =
"hungry"
else:
self.__mood =
"sleepy"
# function to return the animal type
def get_animal_type (self):
return self.__animal_type
# function to return the name
def get_name (self):
return self.__name
# function to rteurn the mood
def check_mood(self):
return self.__mood
#end of class Animal
#end of Animal.py
Code Screenshot:
'''
animalGenerator.py
Python version : 3.6
Python program to implement class Animal
'''
from Animal from Animal
import random
def main():
# display the welcome header
print('Welcome to the animal generator!')
print('This program creates Animal objects.')
animalList = [] # list to store the animals
# loop continues till the user wants
while True:
# input of animal type and
name
animal_type = input('What type of
animal would you like to create? ')
name = input("What is the animal's
name? ")
animal = Animal(animal_type,name) #
create an animal
animalList.append(animal) # add the
animal to the list
choice = input('Would you like to
add more animals (y/n)? ')
if choice.lower() == 'n':
break
# display the animal information
if(len(animalList) == 0):
print('Empty Animal list')
else:
print('Animal List:')
for animal in animalList:
print(animal.get_animal_type()+" the "+animal.get_name()+" is
"+animal.check_mood())
#call the main function
main()
#end of program
#end of animalGenerator.py
Code Screenshot:
Output:
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------
Get Answers For Free
Most questions answered within 1 hours.