POUNDS undefined?

Hello, I am new to python but I don’t understand why POUNDS in undefined. Everything seems to add up. I am new to python and am learning for school, sorry if this is a stupid question. also can someone tell me how the round up command works? couldn’t seem to figure that one out.

#BMI calaculator

#input
def whatis():
    POUNDS = float(input("How much do you Weight in pounds? : "))
    inches = float(input("How tall are you inches? : "))

whatis()

#processing

Numerator = (POUNDS * 703)
Denominator = (inches * inches)



BMI = (Numerator / Denominator )

#output

round(float (BMI, 1))

print ('Your BMI is : ', BMI)

If you assign to a variable in a function, that variable is considered to be local to the function unless you declare, in the function, that it’s global.

You do that with the global statement:

def whatis():
    global POUNDS, inches
    POUNDS = float(input("How much do you Weight in pounds? : "))
    inches = float(input("How tall are you inches? : "))

Incidentally, this:

round(float (BMI, 1))

returns its result, which is then discarded.

BMI itself will be unchanged.

Quoting from the uk nhs Calculate your body mass index (BMI) for adults - NHS

The BMI is calculated by dividing an adult’s weight in kilograms by their height in metres squared.

You will need to convert the units to metric units.

Some things to consider.

  • defining function usually means that it’s needed more than once
  • function usually have explicit return, otherwise it returns None
  • it is good practice to validate user input, otherwise it is easy to blow up the program

There is two questions asked and function with user input validation can be written like that:

def ask(question):
    while True:
        from_user = input(question)
        try:
            answer = float(from_user)
        except ValueError:
            print(f"Expected float but {from_user!r} was entered.")
        else:
            return answer

This function can be utilized twice to get needed data:

weight = ask("Enter your weight in pounds: ")
height = ask("How tall are you in inches: ")

That’s why the calculation includes multiplying by 703; it’s the scaling factor for converting from lb/in2 to kg/m2.

2 Likes

I was wonder what the magic 703 was all about.

As a code review feedback that 703 should be defined as an named constant.
Or the code should convert to Kg and m as used in the BMI definition.

the code isn’t actually supposed to be a bmi converter, it was more so an excercise to demonstrate understanding.