时间:2021-07-01 10:21:17 帮助过:279人阅读
def recur_fibo(n): """递归函数输出斐波那契数列""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # 获取输入 nterms = int(input("您要输出几项? ")) # 检查输入的数字是否正确 if nterms <= 0: print("输入正数") else: print("斐波那契数列:") for i in range(nterms): print(recur_fibo(i))
#输出前20个斐波那契数列到列表 a = 0 b = 1 i = 0 fibo = [] while True: i += 1 if i <= 10: fibo.append(a) fibo.append(b) a += b b += a else: print(fibo) print(len(fibo)) break
以上就是详解python输出斐波那契数列的详细内容,更多请关注Gxl网其它相关文章!