Atsuko
Atsuko

Reputation: 11

For and lists in Python

I've a problem with Python. I've this main:

def main():
    cats = []
    for x in range(1, 11):
        x = Cat(x)
        cats.append(x)  


    listPussa = []
    for x in range(1,21):
       x = Pussa(x)
       listPussa.append(x)


    PussaIndex = 0
    for cat in cats:
        num = cat.getcatNum()

        if num % 2 != 0:
            for i in range(4):
                cat.addPussa(listPussa[PussaIndex])
                PussaIndex += 1

    for cat in cats:
      print cat.mixeta()

The problem is that when I use this function in the main, to print the list of cats:

def mixeta(self): #This is a method of the class Cat, it returns a few strings depending on how many objects Pussa has the object. 
    if len(self.__listPussa) is 0:  #When the cat has no one, it returns that
        return "Miau!", self.getcatNum(), "La llista de pusses esta buida" #The string menas that the list of Pussa is empty.

    else: #When it has at least 1
        listIdPussa = [] #I created a new list 
        for pussa in self.__listPussa: #And for every objcet Pussa in the list of Pussa
            listIdPussa.append(pussa.getIdPussa()) #I save every idPussa in the list.

        return  "Miau!", self.getcatNum(), "El gat esta infectat. El nombre total de pusses es ", len(self.__listPussa), listIdPussa #So, at the end it returns "Meow", the number of the cat, and it says that the cat is "infected" and the total number of objects Pussa is X and each id from every object

The result it has to be that:

('Miau!', 1, 'El gat esta infectat. El nombre total de pusses es ', 4[0,1,2,3])

('Miau!', 2, 'La llista esta buida') # It means the list has no objects Pussa

('Miau!', 3, 'El gat esta infectat. El nombre total de pusses es ', 4[4,5,6,7])

('Miau!', 4, 'La llista esta buida')

And this way until it reach Miau, 10.

But the problem is that my main prints that:

('Miau!', 1, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 2, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 3, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

('Miau!', 4, 'El gat esta infectat. El nombre total de pusses es ', 20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

And that for all cats. What can I do?

Upvotes: 0

Views: 108

Answers (1)

Cilyan
Cilyan

Reputation: 8471

This probably happens because you declared __listPussa as a class member and not as object member. In this case, it is shared between all the instances of Cat.

class Cat:
    __listPussa = []

    def __init__(self, num):
        self.num = num

Each time you add some Pussa to cats, their are added to the same variable shared accross all cats (yeek, cats share their Pussa... :) )

To achieve what you want, you need to make __listPussa an instance member, like this:

class Cat:

    def __init__(self, num):
        self.num = num
        self.__listPussa = []

Upvotes: 1

Related Questions