为什么python递归返回无值

时间:2016-04-18 11:03:05

标签: python recursion nonetype

我试图在Python中使用递归解决一些编码任务 有人可以查看此代码并告诉我它返回None的原因吗?我想在名为" solution"的变量中得到[4, 5]

def rec_fun(upstream,downstream):

    if not upstream or not downstream : 
        if upstream : 

            return  upstream
        if downstream: 
            return downstream
    if upstream[0] >downstream[0] : 
        downstream.pop(0)
    else:
        upstream.pop(0)

    rec_fun(upstream,downstream)


def solution(A, B):

    upstream=[]
    downstream=[]
    n=len(A)
    for i in range(0,n) : 
        if B[i]==0:
            upstream.append(A[i])
        else:
            downstream.append(A[i])
    upstream=sorted(upstream)
    downstream=sorted(downstream)


    return rec_fun(upstream,downstream)


A=[4,3,2,1,5]
B=[0,1,0,0,0]

solution =  solution(A, B)
print solution

输出为:output = None,应为[4, 5]

1 个答案:

答案 0 :(得分:0)

在你的递归函数中,你不是return任何东西。您必须添加一些return语句。即:

return rec_fun(upstream,downstream)