python求斐波那契数列第n项

def fbnq(k):
if k in [1,2]:
return 1
else:
list = [0,1,1]
for i in range(3,k+1):
list.append(list[i-1]+list[i-2])
return list[-1]
n = int(input(‘请输入一个不超过50的数:’))
print(fbnq(n))

def fbnq(k):
a = b = c = 1
while k > 2:
a = b
b = c
c = a+b
k -= 1
return c
n = int(input(‘请输入一个不超过50的数:’))
print(fbnq(n))

你可能感兴趣的:(python,python)