This function is part of a program to convert numbers between different bases.

num = 32
b1 = 10
b2 = 2
x = 1
 
def increasex(num,b2,x):
    if num%(b2**x) < num:
        x = x+1
        increasex(num, b2, x)
    else:
        print x
        return x
 
print increasex(num,b2,x)

The parameters are the number you wish to convert, the base you are converting to, and the value of the exponent (to figure the place, i.e. the 2**x place) . The x value will tell another function where to start dividing, (i.e. 32/2**6, followed by 32/2**5, and so on).

The function will print the value of x, which is 6 with the current values, but will not return x. Why? This is driving me crazy. Any help is greatly appreciated.

Recommended Answers

All 7 Replies

I am scratching my head on this one! However, this modifiecation seems to work correctly ...

num = 32
b2 = 2
x = 1

def increasex3(num, b2, x):
    while num%(b2**x) < num:
        x = x+1
        increasex3(num, b2, x)
    #print x  # test
    return x

result = increasex3(num, b2, x)
print '-->', result

I think there is a logic flaw in either code, since you are not using the result of the recursion call. It seems to pile up in a return stack somewhere! If you uncomment Bumsfeld's internal print statement, it will print a whole bunch (counted 31) of sixes

def increase(num, b2, x):
    if num%(b2**x) < num:
        return increase(num, b2, x+1)
    print x
    return x

heehee ... it took me a while, too. In the end, of course, return was working perfectly. The trick was that the recursion stack was 6 levels deep, and only the top level returned anything. This works now:

def increasex(num,b2,x):
    print "num, b2, x", num, b2, x
    if num%(b2**x) < num:
        x = x+1
        #  increase(num, b2, x)  ## original
        return increasex(num, b2, x)
    else:
        print x
        return x

Go through the original code with num = 32, b1 = 10, b2 = 2, and x = 5 ... that is, the next-to-last time through the recursion. It hits the if statement, takes the 'if' branch, adds 1 to x, and calls increasex(32,10,6). increasex(32,10,6) hits the if statement, takes the 'else' branch, returns 6 to the caller...which was increasex(32,10,5)! But that's done, so it drops out of the conditional, comes to the end of the function, and returns None to the caller ... which was increasex(32,10,4) ... which returns None to increasex(32,10,3) ... and so on.

Moral: recursive functions should always return something. :)

Jeff

Personally, i would use a loop for this.

num = 32
b1 = 10
b2 = 2
x = 1
while 1:
    if num%(b2**x) < num:
        x = x + 1
        continue
    else:
        break
print x

Thanks all for your help. I see now that in order to return a value I have to have the return statement in front of the recursive call to return the value to the previous caller. jrcagle's modification is good. And i like the alternative that mawe posted. thanks again

Ghostdog got me thinking: to recurse or not to recurse? Probably a matter of taste:

def convert(n, base):

    s = ""
    while n >= base:
        n,r = divmod(n, base)  # repeated divisions have the effect of dividing by powers of base
        s = str(r) + s
    else:
        s = str(n) + s
    return s

def convert_recurse(n, base):

    if n < base:
        return str(n)
    else:
        n,r = divmod(n,base)
        return convert_recurse(n,base) + str(r)
    
print convert(254,2)  # 11111110
print convert_recurse(254,2)
print convert(85,3)   # 10011
print convert_recurse(85,3)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.