我想添加一条线,它将计算您可以增加多少公斤的体重才能保持"最佳 BMI"。有人可以帮助我吗?
name = input('Enter your name: ')
height = int(input('Your height?(in cm) '))
weight = int(input('Your weight?(in kg) '))
bmi = float(round(weight/(height/100)/(height/100),1))
#current_weight = float(round(bmi*(height/100)*(height/100),1))
#max_weight to keep bmi < 24.9
print('n''n''Hey',name,'n''Your Body Mass Index (BMI) is:',bmi)
if bmi < 18.5:
print(name,'you are underweight!')
if 18.5 < bmi < 24.9:
print(name,'your weight is OK !')
if 24.9 < bmi < 29.9:
print(name,'you are overweight!')
if bmi > 29.9:
print(name,'you are obese!')
#print('you can put on another',x,'kilograms, don't worry!')
这很简单,与你对BMI公式的理解有很大关系。符合您要求的代码如下:
maxweight = (height**2/10000)*24.9
dif = round(abs(maxweight-weight),2)
print(name+", you have",dif,"kilograms to go until you reach the maximum normal weight")
这适用于权重不足和超重值,始终使用函数 abs()
返回正值。
或者,您可以使用一个函数,它可以更好地处理这两种情况:
def getDifferenceString(name,weight,height):
maxweight = (height ** 2 / 10000) * 24.9
if maxweight<weight:
return "You should go to the gym, "+name+", because you are "+str(round(abs(maxweight-weight),2))+" over the maximum normal weight"
else:
return "No worry, "+name+", you can put on " + str(round(abs(maxweight - weight), 2)) + " more kilos"
print(getDifferenceString(name,weight,height))
<小时 />解释:
-
maxweight
表示直接来自 BMI 公式的最大正常体重 -
dif
是人与maxweight
weight
之差的绝对值,四舍五入到小数点后2位
希望这有帮助!