Home > Back-end >  Use a function to check condition and then output another function
Use a function to check condition and then output another function

Time:10-19

I'm creating a program where a zoo is made and a function to feed the animals is used to check if they are nocturnal and then activate the "eat" function.

When I run the code, it seems to loop through all animals regardless of if they are nocturnal even though there is an if statement to check, but it should only loop through the ones that aren't and output the outcome of "eat" for those.

I suspect the "eat" function is the cause, but I'm not sure why.

Any ideas on how to fix it?

class Animal:

    def __init__(self, name, weight):
        self.name = name
        self.weight = weight


class Elephant(Animal):

    def __init__(self, name, weight):
        super().__init__(name, weight)
        self.species = "elephant"
        self.size = "enormous"
        self.food_type = "herbivore"
        self.nocturnal = False


class Tiger(Animal):

    def __init__(self, name, weight):
        super().__init__(name, weight)
        self.species = "tiger"
        self.size = "large"
        self.food_type = "carnivore"
        self.nocturnal = True


class Racoon(Animal):

    def __init__(self, name, weight):
        super().__init__(name, weight)
        self.species = "racoon"
        self.size = "small"
        self.food_type = "omnivore"
        self.nocturnal = True


class Gorilla(Animal):

    def __init__(self, name, weight):
        super().__init__(name, weight)
        self.species = "gorilla"
        self.size = "large"
        self.food_type = "herbivore"
        self.nocturnal = False


zoo = []


def add_animal_to_zoo(animal_type, name, weight):
    if animal_type == "elephant":
        animal = Elephant(name, weight)
        zoo.append(animal)
        return zoo
    elif animal_type == "racoon":
        animal = Racoon(name, weight)
        zoo.append(animal)
        return zoo
    elif animal_type == "gorilla":
        animal = Gorilla(name, weight)
        zoo.append(animal)
        return zoo
    elif animal_type == "tiger":
        animal = Tiger(name, weight)
        zoo.append(animal)
        return zoo


add_animal_to_zoo("elephant", "john", 111)
add_animal_to_zoo("elephant", "shaun", 200)
add_animal_to_zoo("racoon", "larry", 2)
add_animal_to_zoo("racoon", "jim", 4)
add_animal_to_zoo("gorilla", "jack", 400)
add_animal_to_zoo("tiger", "sarah", 500)
add_animal_to_zoo("tiger", "lucy", 300)
add_animal_to_zoo("tiger", "liam", 250)


def sleep(self):
    if self.nocturnal:
        print("This animal sleeps at night")
    else:
        print("This animal sleeps during the day")


def eat(food):
    for animal in zoo:
        if food == "plants":
            if animal.food_type == "herbivore" or animal.food_type == "omnivore":
                print(f'{animal.name} the {animal.species} thinks {food} is yummy!')
            else:
                print(f'{animal.name} the {animal.species} does not eat {food}')
        elif food == "meat":
            if animal.food_type == "carnivore" or animal.food_type == "omnivore":
                print(f'{animal.name} the {animal.species} thinks {food} is yummy!')
            else:
                print(f'{animal.name} the {animal.species} does not eat {food}')


def feed_animals(time='day'):
    for animal in zoo:
        if time == 'day':
            if not animal.nocturnal:
                eat("plants")
            else:
                eat("meat")
        else:
            return None
        if time == 'night':
            if animal.nocturnal:
                eat("plants")
            else:
                eat("meat")
        else:
            return None


feed_animals()

CodePudding user response:

I suspect the "eat" function is the cause, but I'm not sure why.

The eat(food) function gets as parameter food and loops over all animals instead of taking in addition to food also animal as parameter (eat(animal, food)), so it can work without looping over all animals (skip for animal in zoo: in this function) as this is already done in the feed_animals function.

  • Related