I've been banging my head at a program which repeatedly chooses the largest item in a list and copies it over the entire list, adding a bit of variation each time. It feels like it should be easy, but one element ends up blank every time. Here's the code, with the important part separated:
import random as r
alice = []
bob = []
cathy= []
davos = []
ezekiel = []
francesco = []
graham = []
genVar = 0.25
classroom = [alice, bob, cathy, davos, ezekiel, francesco, graham]
for i in classroom:
skill = r.random()
i.append(skill)
i.append(1-skill)
a = 0
while a < 10:
best = max(classroom, key=lambda x:x[0]*x[1])
for i in classroom:
i.clear()
i.extend(best)
print(best)
i[0] += r.uniform(-1*genVar, genVar)
i[1] += r.uniform(-1*genVar, genVar)
a+=1
print(best)
When I run the cde, the error I get is like so:
[0.5598973199094259, 0.44010268009057407]
[0.5598973199094259, 0.44010268009057407]
[0.5598973199094259, 0.44010268009057407]
[]
Traceback (most recent call last):
File "C:/Users/Leafy/AppData/Local/Programs/Python/Python36-32/multiplierlearner.py", line 24, in <module>
i[0] += r.uniform(-1*genVar, genVar)
IndexError: list index out of range
The weird thing is, when I run the code a second time, I get this result instead:
[0.6459806429424123, 0.3540193570575877]
[]
Traceback (most recent call last):
File "C:/Users/Leafy/AppData/Local/Programs/Python/Python36-32/multiplierlearner.py", line 23, in <module>
i[0] += r.uniform(-1*genVar, genVar)
IndexError: list index out of range
The number of completed elements changes every time, and it's driving me insane. Any help is appreciated, and let me know if there's any more information I should put up.