递归求斐波那契数列前n项python
发布网友
发布时间:2022-05-09 22:19
我来回答
共2个回答
热心网友
时间:2022-04-18 23:40
def fib_th(n):
"""输出第N个斐波那契数列值"""
a, b = 0, 1
t = 2
if isinstance(n, int):
# n = int(n)
assert n >= 1, "序号值必须大于0"
if n == 1:
return a
while t < n:
a, b = b, a + b
t += 1
else:
return b
else:
return "There is not a Number input,please try again."
if __name__ == "__main__":
fib_list = []
for i in range(1, 11):
fib_list.append(fib_th(i))
print(fib_list)
热心网友
时间:2022-04-19 00:58
这~搜个递归python斐波那契或者上楼梯问题