wapadunk
wapadunk

Reputation: 25

Recursive Function to Calculate Exponential without " * "

Write a function power that accepts two arguments, a and b and calculates a raised to the power b. Example: power(2,3) = 8 Raise a TypeError with the message Argument must be integer or float if the inputs are anything other that ints or floats. Note: Don't use **

My code below is not given me my desired output. Please what could be wrong?

def power(a, b):
    if a == type(int) or a == type(float) and b == type(int) or b == type(float):
        def add(a, b):
            num = a
            for i in range(b):
                num += 1
            return num

        def multiply(a, b):
            num = 0
            for i in range(b):
                num = add(num, a)
            return num

        def power(a, b):
            num = 1
            for i in range(b):
                num = multiply(num, a)
            return num
    else:
        return "Argument must be integer or float"

Upvotes: 0

Views: 1188

Answers (1)

Blckknght
Blckknght

Reputation: 104752

There are a bunch of issues with your code.

To start with, your type checking is fractally wrong (it keeps getting more wrong the more deeply you look into it). You should probably use isinstance(a, (int, float)) and isinstance(b, (int, float)) rather than the backwards stuff you have now. Currently you're checking if a and b are equal to the class type (which is the type of int and float). You also have the expression grouping wrong, as the and operator binds more tightly than the ors do (your code does a or (b and c) or d when you wanted (a or b) and (c or d)).

Next, you're defining new functions do do your power calculation, but you never call them from the outer power function (so they'll never do anything). If you want to keep the inner functions, you need a return power(a, b) call somewhere. You could however simplify things a bunch by getting rid of the inner functions and just doing the computation directly in some nested loops. Nested functions are only very rarely necessary.

You're also not doing what you're supposed to do in the error case. You're supposed to raise a TypeError, not return a string.

And finally, I think you've made the whole problem much more complicated than necessary. Your assignment says not to use **, but I suspect you're still allowed to use the * and + operators. I'd just do some multiplying in a loop rather than redefining everything from the ground up (which doesn't even work, since your add function needs +=).

One further note: The problem statement doesn't say if b (the exponent) is always going to be a whole number, or if it can have a fractional part. Handling fractional exponents is a lot more difficult than handling whole number exponents, so I'd ask your instructor if that's actually part of the assignment before spending a lot of time on trying to solve it. It may be that you need to support both int and float types, but even if b is a float it will still be a whole number.

Upvotes: 1

Related Questions