0

I have this small function that takes two integers a and b and checks if a is b raised to some exponent. This is the code.

def is_power(a,b):

    if not a%b==0:
        return a%b==0

    elif a/b==1:
       return a/b==1

    else:
        a = a/b
        is_power(a,b)

print is_power(,) 

The problem is that this always returns None no matter what I input.

But if I replace all returns with prints, then they give the correct result, i.e. True or False.

def is_power(a,b):

    if not a%b==0:
        print a%b==0

    elif a/b==1:
       print a/b==1

    else:
        a = a/b
        is_power(a,b)

is_power(,) 

Why does this happen? This is probably a noob question, but I still can't think it out. Thanks

Mishrito
  • 51
  • 2
  • 1
    Are you missing a `return` on the last line of the function? – bbayles Jul 04 '13 at 14:54
  • Yes he is missing a return statement. Also, are you calling the function with no arguments, or were the arguments relatives of Harry Houdini? – John Doe Jul 04 '13 at 14:57
  • @JohnDoe: No the function is not without arguments. I simply didn't put any values in there. – Mishrito Jul 04 '13 at 18:53

2 Answers2

8

You are ignoring the return value of the recursive call, add a return there:

else:
    a = a/b
    return is_power(a,b)

Without the return statement there, your function just ends and returns None instead. The return value of the recursive call is otherwise ignored.

With the return statement, your code works:

>>> def is_power(a,b):
...     if not a%b==0:
...         return a%b==0
...     elif a/b==1:
...        return a/b==1
...     else:
...         a = a/b
...         return is_power(a, b)
... 
>>> print is_power(10, 3)
False
>>> print is_power(8, 2)
True
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks, it does indeed work if I add a return in the last else clause. But I'm still having a little trouble in understanding the flow of execution. If 'a' is a power of 'b' then eventually it'll reach the second clause which has a return statement. And if it isn't then it'll stop at the first if clause which again returns a False. In either case the function ends at one of the first two clauses which each return a value. Why should that 'return' be inserted before the function call in the last else clause? Thanks for the patience – Mishrito Jul 04 '13 at 16:23
  • @Mishrito: You are forgetting that when `is_power()` calls itself, and that call returns, control returns to the previous invocation of `is_power()`, **not** to whatever called the first `is_power()` function. So in your `else` block at the end, control returns to that point when the recursive `is_power()` call is done. At that point, your code discards whatever the recursive call returned. The function ends there, and `None` is returned, effectively ignoring the recursive call result. – Martijn Pieters Jul 04 '13 at 18:24
  • Ah understood. Thanks for the explanation. I'm trying to teach myself python and sometimes it can get tough to wrap my head around such things. Again thanks for the patience – Mishrito Jul 04 '13 at 18:52
5

You forgot to return on the last else clause.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199