2021-11-28

How do change a parameter in a generator using a send method?

I want to change a parameter in my generator, but I don’t know how to do it correctly I think my generator is not working because it works inside while cycle, I want it to bring 0,1,2,3,4,0,1,2,3,4,5,6,7,8,9

def gen(k=None):
    z = 0
    exc = 0
    while True:
        yield z
        z += 1
        if z > 10:
            break
        if k is not None:
            z = k
            exc += 1
        if exc > 1:
            break


gen1 = gen()
for i in gen1:
    print(i)
    if i == 5:
        gen1.send(0)


from Recent Questions - Stack Overflow https://ift.tt/3p8Ho2f
https://ift.tt/eA8V8J

No comments:

Post a Comment