[Python] Generators stop working in the middle of process

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a 2 generators which yield rgb value for pygame. One generator is callin another one. The generated value should float (increase for a while and then decrease for a while) within 0 and 255 limits. The code working for a random number of steps but then at random time brings IndexError: list index out of range. What is wrong?

    from random import randint as r


    #this generator generates two lists of values to float back and forth between 0 and 255 (supposed rgb value)
    def func():
    a=r(0,255)
    b=r(a,255)

    while True:
    yield list(range(a,b))
    a=b
    b=r(0,a)
    yield list(range(b,a))[::-1]
    a=b
    b=r(a,255)

    red_iter=func()
    green_iter=func()
    blue_iter=func()

    # this generator yields one value per its call from lists generated by above func() generator
    def shifter():
    red_counter=0
    red_range=next(red_iter)
    while True:
    print(red_range[red_counter])
    red_counter+=1
    if red_counter>=len(red_range):
    red_counter=0
    red_range=next(red_iter)


    while True:
    print(shifter())


    enter image description here

    Continue reading...

Compartilhe esta Página