python fibonacci recursion review

lis=[]
def fib(depth):
    # if depth==0:
    #     a1=0
    #     a2=1
    #     a3=a1+a2
    # elif depth==1:
    #     a1=1
    #     a2=1
    #     a3=a1+a2
    # else:
    #     fib(depth)= fib(depth-2) + fib(depth-1)
    # lis.append(fib(depth))
    # a1=0
    # a2=1
    # a3=a1+a2
    if depth==1:
        return 0
    elif depth==2:
        return 1
    else:
        return fib(depth-1)+fib(depth-2)
    # else:
    #     fib(depth)=fib(depth-1)+fib(depth-2)
    #     return fib(depth)
    # elif depth==2:
    #     fib(2)=a2
    # elif depth==3:
    #     fib(depth)=a1+a2
    # return lis
try:
    d=int (input('please enter decimal as depth: '))
except ValueError:
    d=1
# print(fib(depth)
for i in  range(1,d+1):
    lis.append(fib(i))
print(lis)

  

原文地址:https://www.cnblogs.com/ezway/p/6418347.html