0

I have a very simple function which summarize the list elements without using sum function or looping technique. Here it is:

def find_sum(mylist):
    def get_value(sublist, counter):
        counter += sublist.pop()
        if sublist:
            get_value(sublist, counter)
        else:
            print counter              # It will print correct result 
            return counter
    my_sum = get_value(mylist, 0)      # But my_sum is None
    return my_sum

Why the emdedded function returns None?

  • You need "return get_value(sublist, counter)" in your "if sublist:", because without return statement, it will evaluate, but not return value. – Nf4r Nov 18 '16 at 15:51
  • O! Thanks! It's working – heavy_drinker Nov 18 '16 at 15:56
  • Possible duplicate of [Python recursion with list returns None](http://stackoverflow.com/questions/2599149/python-recursion-with-list-returns-none) – tobias_k Nov 18 '16 at 15:56

0 Answers0