-2

I'm trying this problem. I don't see the error and i don't kwon why. This is the code that I made:

class Aquarium: 
    def __init__(self):
        self.animales = []   # Atributo de instancia, lista vacĂ­a
        print('An Aquarium has opened!')
        
    def enters(self,animal):
        self.animal= animal
        for i in range(len(self.animales)):
            if animales[i] == animal:
                self.animales.append(animal)
                print("We already have" + self.animal)
            else:
                print(self.animal + 'is a new member of our Aquarium')

I need to create a code using class Aquarium and get this: my_aquarium = Aquarium() An Aquarium has opened! my_aquarium.enters('Turtle') Turtle is a new member of our Aquarium! my_aquarium.enters('Turtle') We already have Turtle.

Barmar
  • 741,623
  • 53
  • 500
  • 612
rosa
  • 1
  • 2

1 Answers1

0

There are several issues:

  • You are missing self. in the if animales[i] == animal: statement. This means that the function is either crashing or using a global variable called animales, and thus returning results unrelated to your object instance.
  • Because you are printing within the loop, you are going to get your messages multiple times (e.g. "We already have" + self.animal for every other animal in the list)
  • You are appending animal when you find it in the list (which would create duplicates, but in fact you're never going to be able to add an animal with that approach).
Alain T.
  • 40,517
  • 4
  • 31
  • 51