Question originally posted in Spanish, on es.stackoverflow.com, by Ezequiel:
I am trying to make a program that removes certain elements from a group of lists if a certain condition passes, and that duplicates them if a certain other happens. But I have the following problem: when the program is in iteration 50 of 100, it marks IndexError and closes.
Here is the code snippet in which I have the problem:
nnl_len = len(neuralNetworkList) # longitud de lista de objetos fitl_copy = fitness # lista de floats best_fitness_reproduced = 0 if worst_fitness < 0 and first_iteration == 0: # I guess this can worst_fitness = 0 # be ignored for i in range(nnl_len): print(i) if fitl_copy[i] < best_fitness: # EXACT LINE OF THE ERROR <------------------ print("I DIED WITH A FITNESS OF ",fitness[i], ", BEING THE LIMIT ",best_fitness) neuralNetworkList.pop(i) x.pop(i) y.pop(i) fitness.pop(i) colors.pop(i) elif fitl_copy[i] == best_fitness and best_fitness_reproduced: print("I DIED BECAUSE A TOP FITNESS CREATURE ALREADY REPRODUCED ",fitness[i]) neuralNetworkList.pop(i) x.pop(i) y.pop(i) fitness.pop(i) colors.pop(i) else: best_fitness_reproduced = 1 for j in range(99): # plus the mother is 100 print("I SURVIVED WITH A FITNESS OF ",fitness[i], ", BEING THE LIMIT ",best_fitness) neuralNetworkList.append(neuralNetworkList[i]) if random.randint(1,3) == 1: neuralNetworkList[i].mutate(i) x.append(width) y.append(height) fitness.append(0) newcolor = [] for h in range(3): newcolor.append(round( colors[i][h]*random.choice((0.9, 1.1)) )) colors.append(newcolor) #except IndexError: # pass # print("I NEITHER DIED NOR REPRODUCED BECAUSE OF AN INDEX ERROR") nnl_len = len(neuralNetworkList) for i in range(nnl_len): x[i] = width y[i] = height fitness[i] = 0 print("population after reproduction:", len(neuralNetworkList))
this is the traceback:
Traceback (most recent call last): File "C:\Users\Ezequiel\Desktop\Archivos pavos\Sublime Text 3\pruebas_phyton.pyw", line 4921, in <module> if fitl_copy[i] < best_fitness: # fitness[i] <= worst_fitness*4 IndexError: list index out of range