You yield each value twice. Once here:
val = (yield i)
and once here:
yield val
You should only yield each value once, and capture the user's input, like so:
def parrot():
val = None
while True:
val = yield val
If you really want to yield the string 'none'
instead of the actual None
object when the user calls next
, you can do that, but it may be a bad idea:
def parrot():
val = None
while True:
val = yield ('none' if val is None else val)