尽管执行return语句,Python递归函数仍未返回

时间:2020-09-03 16:06:01

标签: python python-3.x recursion

我正在使用Python 3.6在Jupyter笔记本中运行以下递归函数

如果列表长度大于n,则该函数以递归的方式将n + 1值从列表中弹出,并将其添加到前n个值中的最小值,直到列表长度等于n。

def queue_time(customers, n,iteration=0):
    print(f'{iteration}: {customers}')
    if n==1: 
        return(sum(customers))
    else:
        if (n < len(customers)):
            n_customer_time = customers.pop(n)
            n_q = customers.index(min(customers[0:n]))
            customers[n_q]+=n_customer_time
            queue_time(customers,n,iteration+1)
        else:      
            print(f"Finished here:{customers}")
            return(max(customers))
            print("This doesn't print")

x = queue_time([5,3,4], 2)
print(x)
print(max([5,7]))

我得到以下输出:

0: [5, 3, 4]
1: [5, 7]
Finished here:[5, 7]
None
7

我真的很困惑为什么我写的这个函数返回None。怎么会这样?

0 个答案:

没有答案