0

How would I write a code that takes 2 to the exponent x? recursively? I have been trying but i have no clue. Here is what I have.

def recursiveExponent(x):
    if x<=0:
        return 1
    return = 2* recursiveExponent(x - 1)

This code does not work at all. What I want to do is have the user input an exponent and recursively calculate 2 to the input exponent.

Examples:

recursiveExponent(2)---> 4, where 2^2 is 4

recursiveExponent(4)----->16, where 2^4 is 16

newoart
  • 17
  • 1
  • 7
    remove `=` after the `return`. If you are interested in recursion, you might want to check [this](http://stackoverflow.com/q/30214531/1903116) as well. – thefourtheye Dec 01 '15 at 03:24
  • This is also going to create an infinite recursion at the moment, because it will keep calling `recursiveExponent` for larger and larger negative values. – DilithiumMatrix Dec 01 '15 at 03:30
  • 2
    @dilithium actually it won't (although it also wont give the mathematically correct answer for a negative or indeed fractional exponent) - it returns 1 for all zero *or negative* `x`. – lvc Dec 01 '15 at 03:35
  • @DilithiumMatrix it will stop at 0 for positive values of x. Btw, by getting larger, I think you meant "smaller" as `-1` is the greatest negative value. – Ozgur Vatansever Dec 01 '15 at 03:38
  • @newoart as thefourtheye said, just remove `=` and you'll be good to go, for positive values of x at least. – Ozgur Vatansever Dec 01 '15 at 03:39
  • @ozgur yeah, I was definitely off on the infinite recursion. Regarding the terminology, [I don't think you are correct](http://english.stackexchange.com/a/102357/23771) - but I'd be interested if you have a reputable source. – DilithiumMatrix Dec 01 '15 at 06:52

0 Answers0