Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading