簡體   English   中英

python:遞歸方法理解

[英]python: recursive method understanding

我在做什么有什么問題? 在該方法中,即使在進入if子句之后,控件也始終返回到else部分:

def recurse(param1, param2):
    is_bool = some_func_that_returns_boolean(param1, param2)
    if is_bool is False:
        return 1 # exit the func
    else:
        recurse(param1, param2)

您需要在函數末尾添加return以明確返回遞歸函數的值。 if not ,最好使用:

def recurse(param1, param2):
    is_bool = some_func_that_returns_boolean(param1, param2)
    if not is_bool:
        return 1 # exit the func
    else:
        return recurse(param1, param2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM