我目前正在尝试创建一个身体质量指数计算器,给出你的体重指数,然后计算出体重差异,以保持健康的体重。
我用来找BMI的方程式是:
bmi = (weight_pounds / height_inches**2) * 703使用同情,我试图找出多少磅需要增加或减少在19-24 BMI范围内。
这就是我对这个方程式的看法:
X = Symbol('X')
W = Symbol('W')
X = solve( W / height_inches**2) * 703
print(healthy_weight)
healthy_weight = X 当代码运行时,它将返回:
以磅计体重:160磅
输入身高(以英寸为单位):66
你的体重指数是: 25.82,也就是说你超重了!
[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,00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
我如何使一个变量作为一个未知数来表示需要得到/失去的磅,然后把它解出来。
发布于 2022-09-26 15:36:06
从技术上讲,你想要解决的不是一个等式,而是一个不等式。也就是说,您可以通过分别处理两个案例来使用不等式来解决这个问题。
如果体重指数高于24,则计算降至24所需的体重,并给出该目标体重与实际体重之间的差异;如果BMI低于19,则计算达到19的体重所需的体重,并给出该目标体重与实际体重之间的差异。--
您似乎也误解了解决函数的工作方式。给定表达式,solve将查找使表达式等于零的值。要求解方程A = B,通常可以使用solve(A - B, [variable to be solved])。还请记住,solve返回解决方案的 list ,即使该列表只包含一个元素。
因此,请考虑下面的代码。
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")然而,正如另一个答案(在我看来,粗鲁地)试图解释的那样,您也可以直接求解感兴趣变量,而不是使用渐近求解函数。也就是说,下面的脚本将导致相同的结果,但效率更高。
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")要像在文章中指出的那样提示用户输入,可以将前两行代码改为
height_inches = float(input("Enter your height in inches: "))
weight_pounds = float(input("Enter your weight in pounds: "))发布于 2022-09-26 14:56:34
西米,真的吗?
weight_pounds = bmi * height_inches**2 // 703https://stackoverflow.com/questions/73855953
复制相似问题