[英]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.