python 例程的一个好例子

 

用例程来写一个求平均值的算法

#!/usr/local/python/bin/python3

def FunCore():
    total=0
    counter=0
    average=None
    tmp=yield average
    while True:
        total=total+tmp
        counter=counter+1
        average=total/counter
        tmp=yield average

if __name__ =="__main__":
    fc=FunCore()
    next(fc)
    for x in range(10):
        average=fc.send(x)
        print(average)

 

posted on 2017-08-01 09:52  蒋乐兴的技术随笔  阅读(563)  评论(0编辑  收藏  举报

导航