0
def main():

    base = input('Please Enter An Integer for the Base: ')
    exponent = input ('Please Enter An Exponent: ')

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

main()

SAMPLE OUT:

Enter an integer for the base: 2
Enter an integer for the exponent: 5
2 to the power 5 equals 32
3
  • So what is your question? Commented Jun 29, 2015 at 19:40
  • I was wondering how to print out the answer "2 to the power of 5 equal 32" Sorry new to posting something... Commented Jun 29, 2015 at 19:42
  • 1
    You have a bug in your power function. Think about what your function would return if you had exponent == 0 and base == 5. Now think about what the function should return. Commented Jun 29, 2015 at 19:47

2 Answers 2

1

If this is Python 3, use string formatting like this:

result = power(base, exponent)
print("{} to the power of {} equals {}".format(base, exponent, result))

In Python 2,

print "%d to the power of %d equals %d" % (base, exponent, result)
Sign up to request clarification or add additional context in comments.

4 Comments

def main(): base = input('Please Enter An Integer for the Base: ') exponent = input ('Please Enter An Exponent: ') def power (base, exponent): if exponent == 1: return base else : return base * power(base, exponent - 1) result = power(base, exponent) print("{} to the power of {} equals {}".format(base, exponent, result)) main() .... When I tried to run the program it doesn't show the results
You have your code in the wrong order. The print statement, and the calculation of the result, should be part of the main function, which comes before the definition of the power function.
I've added the result into the main function and I'm able to enter the integer and the exponent but as the program is running i keep on getting this error "TypeError: unsupported operand type(s) for -: 'str' and 'int'
This is likely a problem with the types returned by the input() method. You must learn to diagnose and fix your own error messages!
0

This is sort of working for me, but I get 1000 if I use 10 as base and 2 as the exponent. Trying to figure it out now why the math is wrong.

def main():

   base = float(input('Please Enter An Integer for the Base: '))
   exponent = float(input('Please Enter An Exponent: '))
   result = power(base, exponent)
   print("{} to the power of {} equals {}".format(base, exponent, result))

def power (base, exponent):

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

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.