简体   繁体   English

python:递归方法理解

[英]python: recursive method understanding

What is wrong with what I am doing?我在做什么有什么问题? In the approach, the control always goes back to the else part even after coming to the if clause:在该方法中,即使在进入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)

You need to add return to the end of the function to explicily return the value of the recursive function.您需要在函数末尾添加return以明确返回递归函数的值。 And better use if not : 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