I need to compare the return value of a recursive function with an integer. Doing recursively the sum of all elements of a list , I have to compare the final sum with an integer "n" giving back TRUE if sum == n , FALSE if sum != n . In addiction to the function have to return FALSE if i'm giving an empty list . Here I report the code to clarify the situation :)
def function(list_of_numbers,int):
if not list:
return false # I have to return false if list is empty.
if len(l) > 0:
return l[0] + function(list_of_numbers[1:],int) # recursive sum of element
# and here i'm stuck !
when
not l
we either got passed an empty list or have reached our base case so comparen
to ourtest
number, if you want an empty list to return True changetest=-1
totest=0
:If you want an empty list to return False you can check how many times the function has been called and either compare n to test or return False :
If you just want to pass a single argument the number to test against: