-3

I am trying to figure out why I am not getting the correct answer for this coding in python. Here is my code so far:

def main():
    base = input('Enter an integer for the base: ')
    exponent = input('Enter an integer for the exponent: ')
    print(base,'to the power', exponent,'equals', power)

def power(base, exponent):
    if exponent <= 1:
       return base
    else:
       return base * power(base, exponent - 1)
main()

When I run the program with a 2 and 5 (base, exponent) I get this:

Enter an integer for the base: 2
Enter an integer for the exponent: 5
2 to the power 5 equals <function power at 0x03DDC300>
>>> 

My question is this: Why am I getting "function power at 0x03DDC300" or similar answers instead of the correct answer of 32?

tdelaney
  • 73,364
  • 6
  • 83
  • 116
Thomas
  • 1
  • 1
  • 2
    `print(power)` <- what do you expect this to do? – Joel Cornett Oct 03 '15 at 04:50
  • Call a function: `power()`. Using just `power` only gives the name. "function power at 0x..." is exactly that, the name: a function called power with an address at 0x... –  Oct 03 '15 at 04:51
  • Possible duplicate of [In Python, what does '' mean?](http://stackoverflow.com/questions/19333598/in-python-what-does-function-at-mean) – tripleee Oct 03 '15 at 08:36

4 Answers4

1

You need to call the function power with proper integer arguments to get the correct output.

print(base,'to the power', exponent,'equals', power(int(base), int(exponent))) # call the function `power`

Without this, power would just return a callable.

In [1]: def some_func():
   ...:     return 2 
   ...: 

In [2]: print some_func # print the function without calling 
<function some_func at 0x601a050> # returns a callable

In [3]: print some_func() # call the function
2
Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
0
print(base,'to the power', exponent,'equals', power)

Look at the line. You are not calling the function, rather you are just writing function name.

You need to call the function.

Change power to power(base,exponent)

For example, if you want to calculate 2 to the power 3, change the above line like that:

print(base,'to the power', exponent,'equals', power(2,3)) 
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
0

Because power() is a function, so you need call it, not just print it.
And your function power() need two arguments, so you can try the following code:

def main():
    base = int(input('Enter an integer for the base: '))
    exponent = int(input('Enter an integer for the exponent: '))
    # use `int()` function if you wish user enter a intege


    print(base,'to the power', exponent,'equals', power(base, exponent))

def power(base, exponent):
    if exponent <= 1:
       return base
    else:
       return base * power(base, exponent - 1)

main()

Demo:

Enter an integer for the base: 10
Enter an integer for the exponent: 20
10 to the power 20 equals 100000000000000000000
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
0

When you have a function without parentheses, the return value will tell you about the function, not the return value. In order to get the return value, you must plug in the arguments, base and exponent respectively, for the function power().

Also, when one function uses another - say function 1 uses function 2 - you should define function 2 first, not vice versa.

This should work:

def power(base, exponent): if exponent <= 1: return base else: return base * power(base, exponent - 1) def main(): base = input('Enter an integer for the base: ') exponent = input('Enter an integer for the exponent: ') print(base,'to the power', exponent,'equals', power(base, exponent)) main()

Jordan A.
  • 384
  • 1
  • 4
  • 17