Python, recursions in range(a,b)
Python, recursions in range(a,b)
I am trying to finish 1 exercise but i cannot find a solution myself.
I get a number and function should use recursion to find a value:
if n == 1: return 1
elif n != 1: fun(n-1) + n
but i want to provide to my function 2 values, starting point and finishing point and my function finds all values in range(starting point, finishing point).
You're missing the
return statement in the elif statement.– Barmar
Aug 30 at 19:56
return
elif
please show your function and complete code as well as input and expected output.
– Skorpeo
Aug 30 at 23:36
1 Answer
1
Define your function with two parameters. One of them gets idecremented when you make the recursive call, the other is kept the same:
def sumRange(start, end):
if start > end:
return None # or could signal an error
elif start == end:
return start
else:
return sumRange(start, end-1) + end
Don't forget that when you make the recursive call, you have to return the result.
You should have
if start>end and not start<end– Onyambu
Aug 30 at 21:16
if start>end
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What do you mean by "all values". Do you mean the sum?
– coldspeed
Aug 30 at 19:55