python2020. 1. 22. 12:18

def num_coroutine():
    total = 0
    while True:
        print('before yield')
        x = (yield total)
        total += x 
        print('after yield')
        print(x)
    except GeneratorExit:
        print('close coroutine')

co = num_coroutine()
print('total',next(co))
print('total', co.send(int(input('put int:'))))
print('total', co.send(int(input('put int:'))))
print('total', co.send(int(input('put int:'))))

co.close()

 

 

 

 

출처 : https://dojang.io/mod/page/view.php?id=2420

Posted by easy16