Dendecko
Dendecko

Reputation: 1

How do I return my result from this For loop in Python

I am creating an exponent function, it is very simple but I cannot get the function to actually print my result when I give users the ability to give inputs. Here is my function which may have issues to begin with.

#exponent function
      
bas_num = input("Base Number: ")
pow_num = input("Power: ")

def raise_to_power(bas_num, pow_num):
    result = 1
    for index in range(pow_num):
        result = result * bas_num
    return result

as it currently stands I don't get my result back to read. I can't seem to print it based on user inputs. I can make it work if I use

print(raise_to_power(x, y)) 

but then I am giving the inputs, not the user.

any help appreciated.

EDIT

#exponent function

bas_num = int(input("Base Number: "))
pow_num = int(input("Power: "))

def raise_to_power(bas_num, pow_num):
    result = 1
    for index in range(pow_num):
        result = result * bas_num
    return result

print(raise_to_power(bas_num, pow_num))

This worked. Thanks Johnny Mopp

Upvotes: 0

Views: 117

Answers (1)

personaltest25
personaltest25

Reputation: 272

Here is the corrected version of your code. The input function was moved under the def function, as a result, when the function is called like this: raise_to_power() the user will be asked to input bas_num & pow_num.

def raise_to_power():
    bas_num = int(input("Base Number: "))
    pow_num = int(input("Power: "))

    result = 1
    for index in range(pow_num):
        result = result * bas_num
    return result

The output looks like this:

enter image description here

If you find this to solve your question then consider accepting the answer :)

Upvotes: 1

Related Questions