用生成器做斐波那契数列

def f(max):
    n, a, b = 0, 0, 1
    while n <= max:
        yield b
        a, b = b, a + b
        n += 1
    yield 'done'


for i in f(5):
    print(i)

 

posted @ 2018-02-27 10:52  xusuns  阅读(360)  评论(0编辑  收藏  举报