在Python中处理StopIteration

时间:2016-05-24 08:43:51

标签: python loops return iteration stopiteration

我试着编码并在这里获得正确的fitness_point但是在循环之后结果保持显示为0.它首先显示StopIteration,所以我写了尝试除了那里,但结果仍然是错误的.. 我是一个全新的人,我该怎么办?请帮忙..

population={'list_1':[0,2,3,4],'list_2':[2,0,4,1],'list_3':[1,6,0,8],'list_4':[3,5,6,0]}

item = [3,4,2,1]

def get_fitness(items):
    g= iter(items)
    temp=[]
    fitness_point=0
    try:
        for i in items:
            if i != items[3]:
                temp=population['list_{}'.format(i)]
                fitness_point+=temp[g.__next__()-1]
                print(fitness_point)
    except GeneratorExit:
        return 'eror'

return fitness_point

print(get_fitness(item))

0 个答案:

没有答案