迭代法:
def myfun2(n): n1=1 n2=1 while n>2: n3=n1+n2 n1=n2 n2=n3 n-=1 return n3 n = int(input('> ')) re = myfun2(n) print(re)
递归法:
def myfun(n): if n == 1 or n == 2: result = 1 return result else: return myfun(n-1) + myfun(n-2) n=int(input("请输入:")) re = myfun(n) print(re)