I had asked a question earlier that involved loops and lists and received some great feedback. Unfortunately, I've run into a new issue that I just can't seem to solve by myself. My apologies for the large block of code:
import random
from pprint import pprint
petri_dish = []
lst = [y for y in petri_dish if y.status == 1]
turn = 1
class Species:
#__init__,relocate, fight, and target methods
for n in range(20):
petri_dish.append(Species(n,0,0,0,0,1))
def reproduce():
class Offspring(Species):
pass
for z in list(petri_dish):
if z.status == 1 and z.life >= 200:
petri_dish.append(Offspring('A'+str(z.name),0,0,0,0,1))
def move_around():
for x in list(petri_dish):
if turn % 2000 == 0:
reproduce()
x.relocate()
x.target()
while len([y for y in petri_dish if y.status == 1]) > 1:
turn += 1
move_around()
for x in [y for y in petri_dish if y.status == 1]:
pprint(vars(x))
print turn
The idea is to duplicate the "strongest" cells every certain number of turns. The problem is that these cells are being copied too many times; if you run the code a few times, you're bound to see what I'm referring too.
My suspicion is that I'm trying to change a list that I'm iterating over or that I'm somehow incorrectly referencing a list somewhere, but I can't pinpoint the problem spot.
Any and all help would be greatly appreciated. Thank you!