-1

I looked at the different response re: the error indicated on the subject line. I am writing a function which is called recursively. I get unbounded local error on variable ctr.I use a variable ctr to keep count.The code is shown below.

def myfn(N):

    if N == 0:
        return ctr
    elif N % 10 == 2:
        ctr += 1
        A = N/10
        print A
        myfn(A)
    else:    
        A = N/10
        myfn(A)
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Sam
  • 11
  • if I initialize ctr in the funtion, then, when the funtion is recursively called, the ctr value wil get set back to zero . So where do i init ctr – Sam Sep 26 '15 at 15:36

3 Answers3

0

global may be useful.

ctr = 0 # initialize

def myfn(N):
    global ctr

    if N == 0:
        return ctr
    elif N % 10 == 2:
        ctr += 1
        A = N/10
        print A
        return myfn(A) # add return here
    else:    
        A = N/10
        return myfn(A) # add return here

This code is not tested.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Agreed. All I( want to do is initialize ctr inside this function. However, when I recursively call the function the value of ctr will get set back to zero. So ctr will always be zero.So where can I init ctr in the function? – Sam Sep 26 '15 at 15:40
0

I suggest that you shouldn't use ctr and that you should calculate the value for ctr recursively.

def myfn(N):

    if N == 0:
        return 0
    elif N % 10 == 2:
        A = N/10
        print A
        return myfn(A) + 1
    else:    
        A = N/10
        return myfn(A)

This code is not tested.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • It's not customary on SO for one person to post two answers to a question, so you should either delete the previous answer or append the text of this new answer to the old one and delete this one. – PM 2Ring Sep 26 '15 at 16:23
0

I assume that this is an exercise / exploration in handling variables in recursive functions.

You can pass ctr as an argument in the recursive calls, and you can initialize it with a default value so you don't need to supply ctr in the original call.

def myfn(N, ctr=0):
    if N == 0:
        return ctr
    elif N % 10 == 2:
        ctr += 1
        A = N/10
        print A
        return myfn(A, ctr)
    else:
        A = N/10
        return myfn(A, ctr)


print myfn(1233252672)

output

123325267
123325
1233
1
4

(If you need to pass more complex objects around then you have to be careful. See “Least Astonishment” in Python: The Mutable Default Argument for details).

However, it's simpler to just use a while loop for this task. Recursion is not efficient in Python, so you should only use it when it's appropriate to the problem domain, eg working with recursive data structures, like trees.

def myfn(n):
    ctr = 0
    while n:
        a = n // 10
        if n % 10 == 2:
            ctr += 1
            print a
        n = a
    return ctr
Community
  • 1
  • 1
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • Just a question - is it possible to do this functiion w/o a loop? – Sam Sep 26 '15 at 22:09
  • @Sam: Not if you want those intermediate values of A. But if you just want the total count of `2` digits in N, then that's easy to do using a single built-in function call. Of course, that function uses a loop internally. – PM 2Ring Sep 27 '15 at 04:18