Skip to content Skip to sidebar Skip to footer

Iterate Through A List And Delete Certain Elements

I'm working on an assignment in my computer class, and I'm having a hard time with one section of the code. I will post the assignment guidelines (the bolded part is the code I'm h

Solution 1:

At the moment, you are making everything a class attribute, shared amongst all instances of the class, as you use ClassName.attr. Instead, you should use the self.attr to make everything an instance attribute:

classColony(object):def__init__(self):
        self.food = 10self.workerAnts = [] # numWorkerAnts is just len(workerAnts)

Note that you should alter breedWorker to actually add a new Ant instance to the list of self.workerAnts; at the moment, it only increments the count.

To reduce the colony to ants with health, you can use a list comprehension:

self.workerAnts = [ant forantinself.workerAnts if ant.health]

Post a Comment for "Iterate Through A List And Delete Certain Elements"