Recursive function returning none?

后端 未结 1 1697
一整个雨季
一整个雨季 2020-11-28 17:13

I wrote the following function in order to implement my own binary search

def bisect(input, target):
    mid = len(input)/ 2
    if len(input) == 1:
                 


        
相关标签:
1条回答
  • 2020-11-28 17:19

    You are ignoring the return values of recursive calls. You need to explicitly return those too:

    elif input[mid] > target:
        return bisect(input[:mid], target)
    elif input[mid] <= target:
        return bisect(input[mid:], target)
    

    Recursive calls are just like any other function call; they return a result to the caller. If you ignore the return value and the calling function then ends, you end up with that calling function then returning None instead.

    0 讨论(0)
提交回复
热议问题