Home > Enterprise >  Solving for an unknown variable in python
Solving for an unknown variable in python

Time:09-27

I'm currently trying to create a body mass index calculator that gives you your BMI and then calculates the difference in weight needed to be at a healthy weight.

The equation I'm using to find the BMI is:

bmi = (weight_pounds / height_inches**2) * 703

Using sympy, I'm trying to find how many pounds need to be gained or lost to be within the 19-24 BMI range.

This is what I have for that equation:

X = Symbol('X') 
W = Symbol('W') 
X = solve( W / height_inches**2) * 703 
print(healthy_weight) 
healthy_weight = X 

When the code is run it returns with:

Enter your weight in pounds:160

Enter your height in inches:66

Your BMI is: 25.82 Which means you're overweight!

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

How can I make a variable represent pounds needed to be gained/lost as an unknown and then have it solved.

CodePudding user response:

What you're trying to solve is not technically an equation; it is an inequality. That said, you can solve this problem using inequality by handling two cases separately.

  • If the BMI is above 24, calculate the weight needed to get down to 24 and give the difference between this goal weight and the actual weight.
  • If the BMI is below 19, calculate the weight needed to get up to 19 and give the difference between this goal weight and the actual weight.

You also seem to have misunderstood how the solve function works. Given an expression, solve looks for the value that makes the expression equal to zero. To solve the equation A = B, you can often use solve(A - B, [variable to be solved]). Keep in mind also that solve returns a list of solutions, even if that list contains only one element.

So with that said, consider the following code.

import sympy as sp

height_inches = 72
weight_pounds = 200
W = sp.Symbol('W')

bmi = (weight_pounds / height_inches**2) * 703

if bmi > 24:
    goal_weight = float(sp.solve((W/height_inches**2)*703 - 24, W)[0])
    print("Weight loss required:")
    print(weight_pounds - goal_weight)
elif bmi < 19:
    goal_weight = float(sp.solve((W/height_inches**2)*703 - 19, W)[0])
    print("Weight gain required:")
    print(goal_weight - weight_pounds)
else:
    print("Weight is in 'healthy' range")

However, as the other answer has (in my opinion, rudely) attempted to explain, you could also solve for the variable of interest directly instead of using the sympy solve function. To wit, the following script would lead to the same result, but more efficiently.

height_inches = 72
weight_pounds = 200
W = sp.Symbol('W')

bmi = (weight_pounds / height_inches**2) * 703

if bmi > 24:
    goal_weight = 24 * height_inches**2 / 703
    print("Weight loss required:")
    print(weight_pounds - goal_weight)
elif bmi < 19:
    goal_weight = 19 * height_inches**2 / 703
    print("Weight gain required:")
    print(goal_weight - weight_pounds)
else:
    print("Weight is in 'healthy' range")

To prompt user input as you have indicated in your post, you can replace the first two lines of code with

height_inches = float(input("Enter your height in inches: "))
weight_pounds = float(input("Enter your weight in pounds: "))

CodePudding user response:

Sympy, really ?

weight_pounds = bmi * height_inches**2 // 703
  • Related