Commit 03370a98 by BellCodeEditor

save project

parent 9b99a096
Showing with 31 additions and 1 deletions
# 尝试编写func()函数输出斐波那契数列的第十五个数是什么? # 尝试编写func()函数输出斐波那契数列的第十五个数是什么?
# 斐波那契数列:1,1,2,3,5,8,13,21,34,55…… # 斐波那契数列:1,1,2,3,5,8,13,21,34,55……
def func(n): def func(n):
if n<=2:
return 1
else:
return func(n-1)+func(n-2)
i=0
while True:
if func(i)>=22:
print(i)
break
i+=1
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))
...@@ -11,5 +42,4 @@ def func(n): ...@@ -11,5 +42,4 @@ def func(n):
print(func(15)) #调用函数并打印结果
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment