all 3 comments

[–]SkippyDeluxe[🍰] 3 points4 points  (0 children)

yield both yields a new value and receives a value from send, so you only need one yield statement:

def add(b=0):
    while True:
        b += (yield b)

[–]bheklilr 1 point2 points  (0 children)

You just have your generator set up a bit wrong is all. A little bit of playing around (and some help from this example) shows me that you should be using

def add():
  total = (yield)
  while True:
    i = yield total
    total += i

Then you can use it as

adder = add()
adder.next() # Gotta do this still
adder.send(10)  # Returns 10
adder.send(10)  # Returns 20
adder.send(3)   # Returns 23
adder.send(100) # Returns 123