Search code examples
python-2.7returnconditional-statements

Return fails despite everything else in conditional working


I wrote a simple recursive function to test whether a is a power of b.

def is_power(a, b):
""" returns True if a is a power of b"""
    if a == b:
        print('True!')
        return True
    elif a%b != 0:
        return False
    elif a != b and a%b == 0:
        is_power(a/b, b)

When I run it on a case that should return True, it doesn't return anything, even though it executes other commands from the same if statement as the return statement.

is_power(8,2) True!

I assume I'm doing something stupid / missing something blindingly obvious. Can anyone help?


Solution

  • The last alternative is missing 'return'