I have two classes, called "Pussa" and "Cat". The Pussa has an int atribute idPussa, and the Cat class has two atributes, a list of "Pussa" and an int catNum. Every class has some methods, with an __init__
. So, I've to do this
"(f) Now, our cats get a "Pussa" infection. Put 4 Pussa objects on all even-index cats on the list cats. First generate the 20 needed Pussa objects with consecutive numbers. Then copy the references of the Pussa objects to the corresponding cats."
I have done this piece of code, but I don't really know how to go on... I just started this year with Python and I'm lost.
listPussa = []
for x in range(1,21):x = Pussa(x)listPussa.append(x)for cat in cats: #cats is a list of 20 cat objectsif cat.getcatNum()%2 != 0:for i in range(21):cat.addPussa(i)i = i +1
The class Pussa is defined by:
class Pussa:''' Class Pussa '''# (a) Define a private attribute idPussadef __init__(self, idPussa = None):self.__idPussa = idPussa# (b) Define a __str__ method that def __str__(self):self.__idPussa = idPussaif idPussa is None:return "Pussa sin número."else:return "Pussa número = " + idPussa# (c) Define setIdPussadef setIdPussa(self, idPussa):self.__idPussa = idPussa# (c) Define getIdPussadef getIdPussa(self):return self.__idPussa
And the class Cat:
class Cat:''' Class cat '''# (a) Define the init method with a private attribute called catNum and a list of pussesdef __init__(self, catNum = None, listPussa = []):self.__catNum = catNumself.listPussa = listPussa# (b) Define the mixeta method def mixeta(self):if len(self.listPussa) is 0:return "Miau!", self.getcatNum(), "La llista de pusses esta buida"else:for pussa in self.listPussa:return pussa.getIdPussa()return "Miau!", self.getcatNum(), "El nombre total de pusses es ", len(self.listPussa)# (c) Define the setter function for catNumdef setcatNum(self, catNum):self.__catNum = catNum# (c) Define the getter function for catNum def getcatNum(self):return self.__catNum# (d) Define a setter for adding a Pussa in the list of pussesdef addPussa(self, Pussa):self.listPussa.append(Pussa)# (e) Define the cleanCat method which removes all pusses from the cat, empty the listdef cleanCat(self):if len(self.listPussa) is 0:returnelse:for i in range(len(self.listPussa)):self.listPussa.pop(i)
Every help and comment is very helpful :)