Skip to main content
added 141 characters in body
Source Link
melalonso
  • 228
  • 2
  • 9

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

print(recursion(15)

And a better implementation would be:

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

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

print(recursion(15))

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
added 1 character in body
Source Link
melalonso
  • 228
  • 2
  • 9

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+= 1
        return recursion( x - 1)
    return answer

print(recursion(15))

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

print(recursion(15))

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

print(recursion(15))
Source Link
melalonso
  • 228
  • 2
  • 9

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

print(recursion(15))