Why is my raise to power code running without doing anything?

def raise_to_power(base_num, pow_num):
    result = 1
    i = 1
    base_num = float(input("Your base number= "))
    pow_num = float(input("Your Power number = "))
    if pow_num == 0:
        print(result)
    else:
        while i <= pow_num:
            result = result * base_num
            i += 1
    print(result)

When I run the code it’s not even asking for an input, any ideas why?

I played with the indents changed the positioning of the code but that only furthers my problem. I don’t know why it’s not even asking for the input.

>Solution :

def raise_to_power defines the function for later use. By defining functions you can reuse the code inside the function, in case you need it in multiple places in your script.
Defining a function does not execute it. To execute the function you have to call it. In python (and most other languages) you can do this like this:
raise_to_power()

Since your function takes 2 parameters (base_num and pow_num) you have to give the function those. Instead of calling

    base_num = float(input("Your base number= "))
    pow_num = float(input("Your Power number = "))

inside the function, you can request the input outside the function and then give those values to the function call as arguments:

def raise_to_power(base_num, pow_num):
    result = 1
    i = 1
    if pow_num == 0:
        print(result)
    else:
        while i <= pow_num:
            result = result * base_num
            i += 1
    print(result)

base_num = float(input("Your base number= "))
pow_num = float(input("Your Power number = "))

raise_to_power(base_num, pow_num) # Here we actually call (or execute) the function.

Leave a Reply