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