Python Forum
Beginner, my recursion returns None, and related problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Beginner, my recursion returns None, and related problem (/thread-28030.html)



Beginner, my recursion returns None, and related problem - zpacemanzpiff - Jul-02-2020

Absolute beginner at coding attempting to learn Python. Current chapter of "Slither into Python" is on recursion; my current task is to identify the largest number in a set recursively. The author's example works; mine does not. I don't understand why mine doesn't work or why the author's does work. Mine returns "None" despite a few attempts to define my way out of my issue. The author's returns the correct number even though it looks like it's winding up with the wrong one.
import sys

def max_check(arr):       # My version, always returns None
	print(arr) 
	if len(arr) == 1:
		print(arr)        # It looks like the largest number is here!
		return arr[0]     # But now it's gone!
	elif arr[0] < arr[1]: # Not very tidy, but is this approach a problem?
		arr.pop(0)
		max_check(arr)
	elif arr[0] >= arr[1]:
		arr.pop(1)
		max_check(arr)


def maximum(lst):         # Author's version, returns correct number
    print(lst)
    if len(lst) == 1:
        print(lst)        # Why doesn't this print the largest number to console?
        return lst[0]
    else:
        m = maximum(lst[1:])
        if m > lst[0]:
            return m
        else:
            return lst[0]
	

def main():
	print(max_check([1, 2, 3, 2]))
	print(maximum([1, 2, 3, 2]))
	
if __name__ == "__main__":
    main()
Gives me...

Output:
[1, 2, 3, 2] [2, 3, 2] [3, 2] [3] [3] # Mission accomplished? None # Nope. [1, 2, 3, 2] [2, 3, 2] [3, 2] [2] [2] # huh? 3 # huh??
I didn't really know what to search for, because I don't know what I'm failing to understand. Help?


RE: Beginner, my recursion returns None, and related problem - bowlofred - Jul-02-2020

A "set" in python is a specific thing, but you're not using them. You should refer to these as lists instead.

This is happening because in the top-level call, the author's code always returns while yours doesn't.

Imagine calling max_check([1,2]). You'll enter the function and at line 5 since there are two elements in the list, you'll skip the first block. Since 1 < 2, you'll enter the second block.

At line 9, you'll change arr with pop(0), so arr will now be [2]. Then at line 10 you'll call max_check([2]). BUT, you don't do anything with the result of that call. Some answer comes back, and you don't capture it or return it. Instead you get to the end of the function and don't call return. Without this call, the function returns None.

In the other function, the else clause has returns that are reached.


RE: Beginner, my recursion returns None, and related problem - zpacemanzpiff - Jul-02-2020

Thank you. I'll work on my terminology. Also, adding return arr at the end of those blocks made my junk work. Please tell me if I'm understanding this correctly: originally, if I put in a list of 2 or more numbers, the list got passed to 8 or 11 repeatedly until it was down to one member. At that point, 5 happens, and that returned the right number...but only to whatever loop recursive step summoned it, not back out to main(). With returns added, the number gets safely passed back up to where I can work with it again. Oof, head hurts.

Frankly, I'm still really hazy on recursion and what's talking to what. The materials thus far made sense as basically being step-a, step-b, do-step-c-until-d-happens sorts of processes. Ultimately, I know that beyond my confusion, this has to simply be more of that, but something about this isn't clicking for me. I'll keep at it, though. Thanks again!