0
def binary_search_aux(v, seq, lo, hi):
    if lo > hi: 
        return False
    mid = (lo + hi) // 2
    if v == seq[mid]:
        return True
    elif v < seq[mid]:
        binary_search_aux(v, seq, lo, mid-1)
    else:
        binary_search_aux(v, seq, mid+1, hi)

def binary_search(v, seq):
    return binary_search_aux(v, seq, 0, len(seq)-1)


lst = [1, 2, 3, 4, 5]
print(binary_search(4,lst))

Could someone please explain why this code is returning None instead of what I intended it to return (True)?

Thanks in advance

Schwaino
  • 1
  • 2
  • 1
    when you recursively call the function you don't `return` the result, do `return binary_search_aux(...)` – Primusa Sep 27 '19 at 01:17
  • 1
    Possible duplicate of [Why does my recursive python function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-python-function-return-none) – Primusa Sep 27 '19 at 01:18
  • Thankyou, that was easy – Schwaino Sep 27 '19 at 01:26

0 Answers0