0
initialValues = input('Please enter a space separated set of values for y, h and g(0): ')

values = []
values.append(initialValues)

theValues = []

for item in values:
    item = item.split(' ')
    for index in range(len(item)):
        item[index] = int(item[index])
    theValues.append(item)

y = theValues[0][0]
h = theValues[0][1]
g0 = theValues[0][2]

def Sumrecur(y,h,g0):
    if y == 0 :
        return g0
    else:
        sum = 0
        for k in range(1,y):
            sum = sum + Sumrecur(k,h,g0)*h
    return sum

Sumrecur(y,h,g0)

this is a function i'm currently working on. I'm confused as the return sum command doesn't work. Is there anything wrong with the code?? Sorry it might sound silly to some of you but I really don't know how to fix this. thanks before!

5
  • Doesnt work ? Are you getting any error messages ? Commented Apr 26, 2017 at 10:15
  • Could you give an example of input data and expected output? Commented Apr 26, 2017 at 10:19
  • i've edited the post for the inputt. and like the expected output would just the sum of the recursion functionn. @PierPaolo Commented Apr 26, 2017 at 12:44
  • i didn't get any error messages also on the python shell. (>>>) i just get that instead. @Abhimanyusingh Commented Apr 26, 2017 at 12:46
  • Although thats not the question , But you are doing way too much work for getting the input . Using y,h,g0 = map(int,raw_input('INPUT ').split(' ')) should suffice . Commented Apr 27, 2017 at 6:42

2 Answers 2

1

Once you've gone 1 recursion deep you need to return multiple times to actually return the value. Basically you've returned the value to the last recursion but this isn't the original one you called so there will be no output. Maybe write to a global variable or print the value instead.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for answering my question!! but i still don't quite understand, can yoh give me an example???
No problem, glad I could help
0

TL;DR : I would try using range(0,y) in your for loop.

BREAKDOWN: Your recursion depends on the value of y and only ends whenever y equals 0 .

Lets see how the recursions are called using an example

STEP 1 Sumrecur(3,1,1) will call Sumrecur(1,1,1) and Sumrecur(2,1,1)

STEP 2a : Sumrecur(1,1,1) calls nothing

STEP 2b : Sumrecur(2,1,1) calls Sumrecur(1,1,1)

STEP 3: Sumrecur(1,1,1) calls nothing

You see what the issue is ? The value of y never equals 0 , so it never enters the

   if y == 0 :
      return g0

I would try using range(0,y) in your for loop.

3 Comments

hello! first of all thank you so much for answering my questions!! howeverr, i also add return sum for the else condition, so i'm just wondering why it doesn't work. it doesn't return me anythingg. I've tried using range(0,y) but it also didnt work :'(
@RebeccaMaria Are you sure you are printing it ? . Sumrecur(y,h,g0) will just call the function . Since you are returning the value from the function you will have to use print Sumrecur(y,h,g0) .
@RebeccaMaria And am pretty sure you will always get 0 as return value if you use range(1,y) . You should try with range(0,y) to get some output .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.