1

I can't figure out why this returns none!

I have a return for every branch...

Thanks in advance.

def recurPowerNew(base, exp):
    '''
    base: int or float.
    exp: int >= 0

    returns: int or float; base^exp
    '''
    if exp<=0:
        return 1
    elif exp%2==0:
        return recurPowerNew(base*base, (exp/2))

        return base*recurPowerNew(base, (exp-1))


print recurPowerNew(7.62,9)  
2
  • It isn't necessary here, but it's often clearer to use if/elif/else, instead of if/elif and then falling off the end of that statement. (That would also have made your actual bug impossible to write.) Commented Dec 18, 2013 at 21:40
  • Because you don't have a return for every branch. You have two returns for the second branch, and none for the third... Commented Dec 18, 2013 at 21:42

1 Answer 1

8

Because of your indentation, the last line of your input is wrong indented.

You should fix it to this:

def recurPowerNew(base, exp):
    """
    base: int or float.
    exp: int >= 0

    returns: int or float; base^exp
    """
    if exp <= 0:
        return 1
    elif exp%2 == 0:
        return recurPowerNew(base*base, (exp/2))

    return base*recurPowerNew(base, (exp-1))

While we're here, I'll like to point that according to PEP-257 it's better to use double quotes than single quotes:

For consistency, always use """triple double quotes""" around docstrings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.