# Author : XueFeng
def fib(max):
n, a, b = 0, 0, 1
while n < max:
# print(b)
yield b
# 加上yield转换成生成器
a, b = b, a + b
# 相当于t=(b, a+b),a=t[0],b=t[1]
# 而不是a=b, b=a+b
n = n + 1
return 'done'
f = fib(10)
g = fib(6)
print('==========Loop Start==========')
while True:
try:
x = next(f)
print('f:', x)
except StopIteration as e:
# 出现StopIteration故障时进行的操作
print('Generator return value:', e.value)
break
print('===========Loop Stop==========')
python_斐波那契生成器
最新推荐文章于 2023-05-12 22:54:09 发布