Python Forum
Zoo class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Zoo class (/thread-22892.html)



Zoo class - saaat - Dec-02-2019

hi'
i need to built a class for zoo
and i need some help

that is the code so far
==================

class Animal:
    def __init__(self,_name,_hunger ):
        self._name =_name
        self._hunger= 0

    class Dog():
        def __init__(self,_name,_hunger ):
            pass
        
    class cat():
        def __init__(self,_name,_hunger ):
            pass
        
    class Skunk():
        def __init__(self,_name,_hunger ):
            pass
        
    class Unicorn():
        def __init__(self,_name,_hunger ):
            pass
        
    class Dragon():
        def __init__(self,_name,_hunger ):
            pass

def main():
  
    for animal in zoo_lst:
        while animal.is_hungry():
             animal.feed()
======================

id have to Write a main function called main that performs the following tasks:

Creates one animal of each type (Unicorn, Skunk, Cat, Dog, and Dragon) and initializes it with the values listed in the table. She keeps the shows of the animals in the list called zoo_lst.
Goes through the animals in the zoo_lst list using the for loop. For each hungry animal, prints its type (type, subdivision name) and its name (name_) and then feeds it until it is no longer hungry (that is, until its degree of hunger is 0).
Instructions: You can use the following code skeleton.

any help how to start ?
and where to put the main def ?


RE: ZOO CLASS - Larz60+ - Dec-02-2019

dictionaries work very well for this type of application
example:
animals = {
    'Skunk': {
        'species': 'mammal',
        'toothed': True,
        'hair': True,
        'breathes': True,
        'legs': 4
    },
    'human': {
        'species': 'mammal',
        'toothed': True,
        'hair': True,
        'breathes': True,
        'legs': 2
    }
}



RE: ZOO CLASS - buran - Dec-02-2019

Please, post full assignment as well as the skeleton as provided to you.


RE: Zoo class - jefsummers - Dec-02-2019

I think this is closer. Assuming the assignment is to teach you the use of classes (pardon the assumption),Dog, Cat, Skunk can all inherit from Animal. I added then a subdivision implied by your question. Since you are passing hunger in the creation of the objects, it makes no sense to then set the value of _hunger to 0 no matter what value you are passing in.
You may want to clean up the class name in the output. Splitting the text on the . might be a good idea.
class Animal:
    def __init__(self,_name,_hunger ):
        self._name =_name
        self._hunger= _hunger

class Dog(Animal):
    subdivision = "Canine"

class Cat(Animal):
    subdivision = "Feline"

class Skunk(Animal):
    subdivision = "Stinky"

doggy = Dog("Spot",2)
kitty = Cat("Terror",0)
pepe = Skunk("Pepe",1)

print(doggy._name,type(doggy),doggy.subdivision,doggy._hunger)
print(pepe._name, type(pepe),pepe.subdivision, pepe._hunger)
Output:
Spot <class '__main__.Dog'> Canine 2 Pepe <class '__main__.Skunk'> Stinky 1