Erasthotenes的递归筛没有返回

时间:2019-05-04 11:18:44

标签: python recursion return nonetype sieve

我实现了一个递归的Erasthotenes筛选器,该筛选器从使用的调试语句看似有效,但返回None

带有调试语句的代码如下:

def sieb2 (iterable, container):
    print("Just called:", iterable, container, '\n')
    if len(iterable) != 1:
        container.append(iterable [0])
        iterable = [item for item in iterable if item % iterable [0] != 0]
        print("New call:", iterable, container, '\n')
        sieb2(iterable, container)
    else: 
        container.append(iterable[0])
        print("Return:", iterable, container, '\n')
        print("Container:", container)
        return container

例如,该函数的调用方式为:

lst = list(range(2, 10) # I might add statements for removing everything <2 and sorting
primes = []

print(sieb2(lst, primes))

此输入的输出如下所示:

# Debug-Statements from the function:
Just called: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] [] 

New call: [3, 5, 7, 9, 11, 13, 15, 17, 19] [2] 

Just called: [3, 5, 7, 9, 11, 13, 15, 17, 19] [2] 

New call: [5, 7, 11, 13, 17, 19] [2, 3] 

Just called: [5, 7, 11, 13, 17, 19] [2, 3] 

New call: [7, 11, 13, 17, 19] [2, 3, 5] 

Just called: [7, 11, 13, 17, 19] [2, 3, 5] 

New call: [11, 13, 17, 19] [2, 3, 5, 7] 

Just called: [11, 13, 17, 19] [2, 3, 5, 7] 

New call: [13, 17, 19] [2, 3, 5, 7, 11] 

Just called: [13, 17, 19] [2, 3, 5, 7, 11] 

New call: [17, 19] [2, 3, 5, 7, 11, 13] 

Just called: [17, 19] [2, 3, 5, 7, 11, 13] 

Mew call: [19] [2, 3, 5, 7, 11, 13, 17] 

Just called: [19] [2, 3, 5, 7, 11, 13, 17] 

Return: [19] [2, 3, 5, 7, 11, 13, 17, 19] 

Container: [2, 3, 5, 7, 11, 13, 17, 19]

# Printed return:

None

从我可以看到的功能正常工作,并返回返回else语句,打印语句也正确执行,并输出最终容器。

为什么返回语句输出NoneType而不输出容器列表?

2 个答案:

答案 0 :(得分:1)

您只是在递归函数中缺少一个return语句:

def sieb2(iterable, container):
    print("Just called:", iterable, container, '\n')
    if len(iterable) != 1:
        container.append(iterable[0])
        iterable = [item for item in iterable if item % iterable[0] != 0]
        print("New call:", iterable, container, '\n')
        return sieb2(iterable, container)  # FIXED
    else: 
        container.append(iterable[0])
        print("Return:", iterable, container, '\n')
        print("Container:", container)
        return container

始终记住要return递归调用这些函数,以使它们的值正确返回到堆栈中。

答案 1 :(得分:0)

def sieb2 (iterable, container):
    print("Just called:", iterable, container, '\n')
    if len(iterable) != 1:
        container.append(iterable [0])
        iterable = [item for item in iterable if item % iterable [0] != 0]
        print("New call:", iterable, container, '\n')

        # Added return
        return sieb2(iterable, container)
    else: 
        container.append(iterable[0])
        print("Return:", iterable, container, '\n')
        print("Container:", container)
        return container

lst = list(range(2, 10)) # I might add statements for removing everything <2 and sorting
primes = []

r = sieb2(lst, primes)
print('r ', r)

更新的内容:
return sieb2(iterable, container)