简单版
def fib(max):
n,befor,after=0,0,1
while n<max:
print(after)
# temp=befor
# befor=after
# after=temp+after
# befor=after
befor,after=after,befor+after
n=n+1
fib(10)
递归版
def fib2(max):
if max == 0:
return 0
elif max == 1:
return 1
else:
result=fib2(max-1)+fib2(max-2)
return result
生成器版
def fib(max):
n,befor,after=0,0,1
while n<max:
print(befor)
befor,after=after,befor+after
n=n+1
g=fib(10)
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))