0
def list_min(lst, low, high):
    if high == low:
        #### IT prints the value that I want when I do : "print(lst[low]) or print(lst[high])"
        return lst[low]
    elif lst[low] >= lst[high]:
        list_min(lst, low+1, high)
    else:
        list_min(lst, low, high-1)

lst1 = [1, 2, 3, 4, 2, 3, 1, 2, 34, 44]

print(list_min(lst1, 0, len(lst1)-1))

Shayan
  • 5,165
  • 4
  • 16
  • 45
MSS
  • 1
  • 2
  • 5
    You're only calling `return` in one of three branches. You need to `return list_min(lst, low+1, high)` and `return list_min(lst, low, high-1)` in the other two. – Charles Duffy Sep 16 '21 at 20:01
  • Keep in mind that `return` only returns a value to the thing that's one step up the call stack. If _that_ thing doesn't `return` the value to _its_ parent, the value is lost. – Charles Duffy Sep 16 '21 at 20:03
  • @CharlesDuffy can you explain more about `return` behavior? Today i got the problem of getting `None` from a function and finally i didn't realize why it happened. But it was like this case, a recursive function. – Shayan Sep 16 '21 at 20:08
  • @CharlesDuffy thank you! – MSS Sep 16 '21 at 20:11
  • @Shayan it's simple, if your function exits without hitting a `return` then it implicitly returns `None`. You call it with `list_min(lst1, 0, len(lst1)-1)`, so the first `if` is not true, you then go to the `elif` which is also false, then you go to the `else`. You then make a recursive call, the function exits without hitting a `return` so it returns `None` – juanpa.arrivillaga Sep 16 '21 at 20:13
  • @CharlesDuffy but i had `return` in my code. I think this kind of recursive function make couple af problems sometimes. – Shayan Sep 16 '21 at 20:15
  • @Shayan, I'd need to see the specific problem you hit (and how it differs from the one this question is about) to speak to it. If you ask your own question, feel free to @ me in. – Charles Duffy Sep 16 '21 at 20:16
  • Thank you @CharlesDuffy ! I would ! But it's part of a code so i need to specify whole the thing. So I'll keep in touch with you there. – Shayan Sep 16 '21 at 20:20

0 Answers0