Search code examples
pythonfunctionvariablesscopecall

How to declare variiables within functions in python function error code "name 'male' is not defined"


I want to write a program, that inputs height and gender, and based off of that it tells you what your ideal weight should be given your height and ideal BMI (22 for men, 21 for women).

However, when I call the function, it never works.

Source Code

def BMI(h,g):
    h = int(input("your height \n"))
    g = str(input("input your gender, 'male' or 'female' \n"))
    male = "male"
    female = "female"
    if g == male:     
        w=22*((h)**2)
    if g == female:
        w=21*((h)**2)
    return(w)

error code

"name 'male' is not defined"

Any help is appreciated I am using python 3


Solution

  • If you want to call the function by @MrPrincerawat you can do that with:

    def BMI(h,g):
        if g == "male":     
          w=22*((h)**2)
        elif g == "female":
          w=21*((h)**2)
        return(w)
    

    call with:

    BMI(100, 'male')
    

    if you want to print:

    print(BMI(100, 'male'))
    

    if you want it as a variable:

    weight = BMI(100, 'male')