0
def recursion(x):
    answer = 0
    if( x > 10 ):
        answer +=1
        return recursion( x - 1)
    return answer
recursion(15)

I'm just playing around with python and recursive functions and made this, but strangely having errors.

Why is this printing 0 instead of my expected answer of:

5

3
  • 2
    because answer can only be 1 or 0 ... Commented Feb 12, 2016 at 3:43
  • 1
    What do you mean? I'm returning answer, which started off as 0, but as x > 10, im adding 1 to answer every loop. So first x is 15,then 14,13,12,11 which is 5 loops adding 1 to answer. So shouldn't it return 5? Commented Feb 12, 2016 at 3:45
  • 1
    answer is a local variable. It is declared and set to zero each time recursion() is called. Commented Feb 12, 2016 at 3:49

6 Answers 6

3

you need to add the recursive call to your answer, not return it

def recursion(x):
    answer = 0
    if( x > 10 ):
        answer = answer + 1 + recursion( x - 1)
    return answer

sometimes it is helpful to step through your code ... debuggers do this excellently, however you can also use pythontutor.com to visualize execution.

this will visually show you what happened with your old code and why it was broken

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

Comments

1

You need to make answer a global variable:

answer = 0

def recursion(x):
    global answer
    ...

1 Comment

well thats an interesting solution I would not have thought of +1 since it will be easy for OP to understand ... even if in general you want to avoid globals
1

Because the answer you define is in local scope .

It means the answer in this function call is not the same as in last call .

Your code should be

def recursion(x , answer):
    if( x > 10 ):
        answer +=1
        return recursion( x - 1 , answer)
    return answer
print(recursion(15 , 0))

1 Comment

solid functional programing example solution +1
1

What is happening is that every time you call recursively the function the value of answer is set to 0. You can change it to:

answer = 0

def recursion(x):
    global answer
    if( x > 10 ):
        answer += 1
        return recursion( x - 1)
    return answer

recursion(15)

And a better implementation would be:

def recursion(x):
    if( x > 10 ):
        return 1 + recursion(x - 1)
    return 0

2 Comments

So by typing your global answer on line 4, you're calling answer into the method, which allows for the assignment answer += 1. Oh thanks nice, haven't used globals before
That's right, one thing is that you need to reset answer every time you want to call the function (using this implementation).
0

Cuz your answer is a local variable,every time you call the function,there is a "new answer para",just like that:

recursion(15):answer=0 answer+=1

recursion(14):answer=0 answer+=1

...

recursion(10):answer=0 jump if then return the answer to recursion(11), then to recursion(12)...... the right way for your program should be:

answer = 0
def recursion(x):
    global answer
    if( x > 10 ):
        answer +=1
        return recursion( x - 1)
    return answer

Comments

0

The variable answer allways return 0.

A better solution would:

def recursion(x, answer):
    if( x > 10 ):
        answer +=1
        return recursion( x - 1, answer)
    return answer
recursion(15, 0)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.