I suspect you're getting the error you describe when you randomly select the same Organism
as both parents. You remove it from the list with your first call to list.remove
, but the second one fails, as the Organism
is already gone.
I'm not sure if you intend for it to be possible for the same organism to be picked twice. If so, you need to put a check on the second call to remove
:
if parent2 is not parent1:
population.remove(parent2)
If, on the other hand, you never want to pick the same Organism
twice, you need to change how you're picking your parent
s. Here's a simple fix, though there are other ways to do it:
parent1, parent2 = random.sample(population, 2)