【发布时间】:2019-03-20 01:29:26
【问题描述】:
def k_comp(n):
n_new = 0
if n == 0:
n_new = 2
if n == 1:
n_new == 1
if n > 1:
n_new = (k_comp(n-1) + k_comp(n-2))**2
return n_new
def Kseq(start, stop, step):
""" (int,int,int) -> list of integers
Kseq(0,6,1)--->
[2, 1, 9, 100, 11881, 143544361]
Kseq(2,6,2)---->
[9, 11881]
"""
final_list = []
append_this = 0
for i in range (start,stop,step):
append_this = k_comp(i)
final_list.append(append_this)
return final_list
print(Kseq(0,6,1))
它打印的不是预期的输出:[2, 0, 4, 16, 144, 16384]
代码应该这样做: 输入:此函数传递定义数字序列的 start (>= 0)、stop (>start) 和 step (>= 1) 值。 输出:此函数返回相应 K 序列的列表。 k序列为k(n) = (k(n-1) + k(n-2))^2
【问题讨论】:
-
请修正你的缩进。
-
你犯了逻辑错误,而不是你的程序:D
标签: python recursion compiler-errors logic