ошибка из random.randint(0, len(gen1)-1)

Запускаю код:

import pygame as pg
import random
sc = pg.display.set_mode((660, 660))
obj = []
e = []
gen = []
gen1 = []
cc = 0
i = 0
i1 = 0
i2 = 0
for scan in range(1, 65):
    obj.append([random.randint(0, 32), random.randint(0, 32), 0])
    e.append(99)
    gen.append([random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), 0])
for scan in range(1, 257):
    obj.append([random.randint(0, 32), random.randint(0, 32), 1])
    e.append(-1)
    gen.append(0)
for scan in range(1, 257):
    obj.append([random.randint(0, 32), random.randint(0, 32), 2])
    e.append(-1)
    gen.append(0)
def read(x):
    global i, i1
    i = 0
    i1 = 0
    if x >= 1 and x <= 3:
        i = -1
    if x >= 5 and x <= 7:
        i = 1
    if x >= 3 and x <= 5:
        i1 = 1
    if x >= 7 and x <= 8:
        i1 = -1
    if x == 1:
        i1 = -1
run = True
fps = 6
ck = pg.time.Clock()
while run:
    ck.tick(fps*(1024/len(obj)))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False
    cc = 0
    for scan in obj:
        if scan[2] == 0:
            cc += 1
    if cc <= 8:
        gen1 = []
        for scan in obj:
            if scan[2] == 0:
                gen1.append(gen[obj.index(scan)])
        while len(gen1) < 64:
            gen1.append(gen[random.randint(0, len(gen1)-1)])
        obj = []
        e = []
        gen = []
        cc = 0
        i = 0
        i1 = 0
        i2 = 0
        for scan in obj:
            obj.append([random.randint(0, 32), random.randint(0, 32), 0])
            e.append(99)
            gen.append([random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), 0])
        for scan in range(1, 257):
            obj.append([random.randint(0, 32), random.randint(0, 32), 1])
            e.append(-1)
            gen.append(0)
        for scan in range(1, 257):
            obj.append([random.randint(0, 32), random.randint(0, 32), 2])
            e.append(-1)
            gen.append(0)
    sc.fill((0, 0, 0))
    i2 = 0
    while i2 <= len(obj)-1:
        if obj[i2][2] == 1:
            if [obj[i2][0], obj[i2][1], 0] in obj:
                e[obj.index([obj[i2][0], obj[i2][1], 0])] += 10
                obj.pop(i2)
                e.pop(i2)
                gen.pop(i2)
                i2 -= 1
            else:
                pg.draw.rect(sc, (0, 255, 0), (obj[i2][0]*20, obj[i2][1]*20, 20, 20))
        if obj[i2][2] == 2:
            if [obj[i2][0], obj[i2][1], 0] in obj:
                e[obj.index([obj[i2][0], obj[i2][1], 0])] -= 10
                obj.pop(i2)
                e.pop(i2)
                gen.pop(i2)
                i2 -= 1
            else:
                pg.draw.rect(sc, (255, 0, 0), (obj[i2][0]*20, obj[i2][1]*20, 20, 20))
        if obj[i2][2] == 0:
            if e[i2] < 0:
                obj.pop(i2)
                e.pop(i2)
                gen.pop(i2)
                i2 -= 1
            else:
                if gen[i2][-1] > len(gen[i2])-2:
                    gen[i2][-1] = 1
                read(gen[i2][gen[i2][-1]])
                gen[i2][-1] += 1
                obj[i2][0] += i
                obj[i2][1] += i1
                if obj[i2][0] < 0:
                    obj[i2][0] -= i
                if obj[i2][0] > 32:
                    obj[i2][0] -= i
                if obj[i2][1] < 0:
                    obj[i2][1] -= i1
                if obj[i2][1] > 32:
                    obj[i2][1] -= i1
                if obj.count([obj[i2][0], obj[i2][1], 0]) == 2:
                    obj[i2][0] -= i
                    obj[i2][1] -= i1
                pg.draw.rect(sc, (0, 0, 255), (obj[i2][0]*20, obj[i2][1]*20, 20, 20))
                e[i2] -= 1
        i2 += 1
    pg.display.update()
    obj.append([random.randint(0, 32), random.randint(0, 32), 1])
    e.append(-1)
    gen.append(0)
    if obj.count([obj[i2][0], obj[i2][1], 1]) == 2 or obj.count([obj[i2][0], obj[i2][1], 2]) == 2:
        obj.pop(-1)
        e.pop(-1)
        gen.pop(-1)
    obj.append([random.randint(0, 32), random.randint(0, 32), 2])
    e.append(-1)
    gen.append(0)
    if obj.count([obj[i2][0], obj[i2][1], 1]) == 2 or obj.count([obj[i2][0], obj[i2][1], 2]) == 2:
        obj.pop(-1)
        e.pop(-1)
        gen.pop(-1)
pg.display.quit()

выходит ошибка:

Traceback (most recent call last):
  File "C:\Users\Эдвард\Desktop\test — копия (2).py", line 56, in <module>
    gen1.append(gen[random.randint(0, len(gen1)-1)])
  File "C:\Users\Эдвард\AppData\Local\Programs\Python\Python39\lib\random.py", line 338, in randint
    return self.randrange(a, b+1)
  File "C:\Users\Эдвард\AppData\Local\Programs\Python\Python39\lib\random.py", line 316, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0, 0, 0)

исходит из строки: gen1.append(gen[random.randint(0, len(gen1)-1)]) как это решить?


Ответы (0 шт):