Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zoo class
#1
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 ?
Reply
#2
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
    }
}
Reply
#3
Please, post full assignment as well as the skeleton as provided to you.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020