斐波那契数列的应用

def fib(max):
    n,a,b=0,0,1
    while n < max:
        #print(b)
        yield b
        a,b=b,a+b
        n=n+1
    return "done"
f=fib(10)
g=fib(6)
while True:
    try:
        x=next(g)
        print("g",x)
    except StopIteration as e:   #检查捕捉错误
        print("Generator return value:",e.value)
        break
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print("=====start loop=====")
for i in f:
    print(i)
g 1
g 1
g 2
g 3
g 5
g 8
Generator return value: done
1
1
2
3
5
8
13
21
=====start loop=====
34
55

上一篇:fibnacci数列递归实现--20211322肖权城


下一篇:fibnacci数列递归实现