Help needed with Animal Shelter coding assignment - failing multiple tests

I’m having trouble with my Animal Shelter coding assignment. Specifically, I’m getting error messages when trying to run tests for the doesAnimalExist, GetSpecies, addAnimal, removeAnimal, and removeSpecies methods in my AnimalShelter class. However my Animal.py has passed all its tests. I’ve double-checked my code, and I’m not sure what’s causing the errors. Can anyone offer any advice or suggestions for how to fix the issue? Thank you!
My Error messages:

1. Test AnimalShelter doesAnimalExist method (0/15) Test Failed: False != True
2. Test AnimalShelter GetSpecies Method (0/15) Test Failed: '<Animal.Animal object at 0x7f4dfb20e198>[79 chars]7b8>' != 'Species: DOG, Name: ZORRA, Age: 5, Weight[100 chars]25.0' - <Animal.Animal object at 0x7f4dfb20e198> - <Animal.Animal object at 0x7f4df8d4c898> - <Animal.Animal object at 0x7f4df8d4c7b8>+ Species: DOG, Name: ZORRA, Age: 5, Weight: 8.0 + Species: DOG, Name: CORA, Age: 1, Weight: 12.2 + Species: DOG, Name: MAGGIE, Age: 10, Weight: 25.0
3. Test AnimalShelter addAnimal method (0/15) Test Failed: False != True
4. Test AnimalShelter removeAnimal method (0/15) Test Failed: False != True
5. Test AnimalShelter removeSpecies method (0/10) Test Failed: '<Animal.Animal object at 0x7f4dfb20e160>[79 chars]7f0>' != 'Species: DOG, Name: ZORRA, Age: 5, Weight[100 chars]25.0' - <Animal.Animal object at 0x7f4dfb20e160> - <Animal.Animal object at 0x7f4df8d4c9b0> - <Animal.Animal object at 0x7f4df8d4c7f0>+ Species: DOG, Name: ZORRA, Age: 5, Weight: 8.0 + Species: DOG, Name: CORA, Age: 1, Weight: 12.2 + Species: DOG, Name: MAGGIE, Age: 10, Weight: 25.0"

My Animal.py:

class Animal:
    def __init__(self, species=None, weight=None, age=None, name=None):
        if species != None:
            self.species = species.upper()
        else:
            self.species = None
        self.weight = weight
        self.age = age
        if name != None:
            self.name = name.upper()
        else:
            self.name = None

    def setSpecies(self, species):
        self.species = species.upper()

    def setWeight(self, weight):
        self.weight = float(weight)

    def setAge(self, age):
        self.age = int(age)

    def setName(self, name):
        self.name = name.upper()

    def toString(self):
        return f'Species: {self.species}, Name: {self.name}, Age: {self.age}, Weight: {self.weight:.1f}'

AnimalShelter.py

class AnimalShelter:
    def __init__(self):
        self.AnimalDictionary = {}

    def addAnimal(self, animal):
        if animal is None or animal.species is None:
            return False
        species = animal.species.upper()
        if species in self.AnimalDictionary:
            self.AnimalDictionary[species].append(animal)
        else:
            self.AnimalDictionary[species] = [animal]
        return True

    def removeAnimal(self, animal):
        if animal is None or animal.species is None:
            return False
        species = animal.species.upper()
        if species in self.AnimalDictionary:
            if animal in self.AnimalDictionary[species]:
                self.AnimalDictionary[species].remove(animal)
                return True
        return False

    def removeSpecies(self, species):
        if species is None:
            return False
        species = species.upper()
        if species in self.AnimalDictionary:
            removed_animals = self.AnimalDictionary[species]
            del self.AnimalDictionary[species]
            for animal in removed_animals:
                animal.species = None
            return True
        else:
            return False

    def getAnimalsBySpecies(self, species):
        if species.upper() not in self.AnimalDictionary:
            return False
        species = species.upper()
        if species in self.AnimalDictionary:
            animal_list = self.AnimalDictionary[species]
            animals_str_list = [str(animal) for animal in animal_list]
            return "\n".join(animals_str_list)
        else:
            return None

    def doesAnimalExist(self, animal):
        if not hasattr(animal, "species") or animal.species is None:
            return False
        species = animal.species.upper()
        if species in self.AnimalDictionary:
            if animal in self.AnimalDictionary[species]:
                return True
        return False

# jiajunwang@ucsb.edu

I think we can’t really help without seeing the tests. And usually a
test run will be more verbose than the summary you’ve made, often citing
the lines of the test code which failed etc.

Can you show us the tests and the full test run transcript?

Thanks,
Cameron Simpson cs@cskk.id.au

In order to tell you why the code does the wrong thing, we would first have to know how you try to use the code (i.e., what happens in the test), what is supposed to happen when you try using it that way, and what happens instead.

In Animal.py:

In AnimalShelter.py:

I wonder if, perhaps, this may be part of the problem. Go back to your instructions and see whether a toString method is called for, or whether instead it should be a __str__ method. Either way, these probably should be brought in line with each other.

The assertions are being quite unhelpful. Probably deliberately, to avoid giving away too much information about the challenge. You may need to show more of the instructions you were following.

From the given code, it seems that you are missing the implementations for the doesAnimalExist, GetSpecies, and removeSpecies methods in your AnimalShelter class. I’ll provide you with the missing implementations for these methods.

  1. doesAnimalExist method:

pythonCopy code

def doesAnimalExist(self, animal):
    if animal is None or animal.species is None:
        return False
    species = animal.species.upper()
    if species in self.AnimalDictionary:
        return animal in self.AnimalDictionary[species]
    return False
  1. GetSpecies method:

pythonCopy code

def GetSpecies(self, species):
    if species is None:
        return None
    species = species.upper()
    if species in self.AnimalDictionary:
        return '\n'.join([animal.toString() for animal in self.AnimalDictionary[species]])
    return None
  1. removeSpecies method:

pythonCopy code

def removeSpecies(self, species):
    if species is None:
        return False
    species = species.upper()
    if species in self.AnimalDictionary:
        del self.AnimalDictionary[species]
        return True
    return False

Add these methods to your AnimalShelter class and then rerun your tests.