I am trying to write a simple recursive function to find the maximum in a list without using any built in functions, with the exceptions being 'print' and 'len'. I made a simple linear search using recursion that compares each member of the list to the current Max.
x=[1,2,3]
Max=x[-1]
def Max_list(Max, x, c=2):
if len(x)==1:
return Max
else:
if c==(len(x)+1):
print('hi')
return Max
elif x[len(x)-c]>Max:
Max=x[len(x)-c]
c+=1
Max_list(Max, x, c)
elif x[len(x)-c]<=Max:
c+=1
Max_list(Max, x, c)
print(Max_list(Max, x))
What confuses me is that my program prints 'hi' (this was a verification that my if condition was satisfied), but returns None. I could have made it attempt to return anything, and it would still return 'None'. I am wondering how to fix it obviously, but if anyone could give me an explanation as to why my code will always return None in its current state, that would be wonderful.