【问题标题】:Recursive Functions not return [duplicate]递归函数不返回[重复]
【发布时间】:2016-02-01 09:38:36
【问题描述】:

我正在使用递归函数,我不明白为什么如果列表为空 ([]) 时即使执行了返回之前的打印函数,该函数也不返回。

def go(mylist):
if not mylist:
    print('Empty list')
    return 'List Empty'
else:
    print(mylist)
    mylist.pop()
    go(mylist)

print(go([1, 2, 3, 4, 5]))

我得到 return 'None' 因为没有 return 的函数总是返回 'None'

【问题讨论】:

    标签: python


    【解决方案1】:

    您必须返回从递归调用中收到的值:

    return go(mylist)
    

    【讨论】:

    • 感谢您的快速答复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 2022-08-13
    • 2020-08-24
    • 2022-06-29
    • 2017-01-12
    • 2015-10-22
    相关资源
    最近更新 更多