0

I have this:

def main():

    input1 = input('Enter an integer for the base:')
    input2 = input('Enter an integer for the exponent:')


main()

This is what I fully need to do:

Create a main, and a power function. Half done.

prompt the user to enter an integer for the base of the power. Done.

prompt the user for an integer for the exponent of the power. Done.

call the power function and print its returned value. Can do.

The recursive power function, power(base,exponent), must recursively calculate the value of the power and then return it. Need help with this.

I have been trying to learn the recursive function stuff the past two days, and cannot seem to wrap my head around it. Could someone provide me a walk through of this, so I can mess around with it, so I can understand it better? Thanks!

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Kate Bliss
  • 97
  • 1
  • 1
  • 6
  • 1
    I tried explaining the basics of recursion in [this answer](http://stackoverflow.com/a/30214677/1903116). You might want to go though that once. – thefourtheye May 23 '15 at 16:04

1 Answers1

7

You basically need to multiply a given number to itself until the power reaches zero:

def power(x, y):
    if y == 0:
        return 1

    if y >= 1:
        return x * power(x, y - 1)
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70