【发布时间】:2019-11-16 08:27:46
【问题描述】:
我想根据用户输入生成斐波那契函数 N 次。 (我认为这是一个 while 循环或 if 语句,如 if N in range)。 还有第二个用户输入定义为 Y。Y 表示重复函数的位数,我想计算生成的数字有多少位数是 Y。
以下是我的不完整代码:
N = int(input("Enter N: "))
Y = int(input("Enter Y: "))
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-2) + fibonacci(n-1)
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(fibonacci(i))
提前致谢
【问题讨论】:
-
请举例说明您计划创建的功能的预期输入和输出。
-
@manavhs13 如果答案对您没有帮助,请发表评论,如果解决了您的问题,请将答案标记为已解决。
-
在你的例子中你从不使用
N