绝对的初学者正在寻找解决方案

时间:2017-03-18 12:59:02

标签: python

我想添加一条线来计算你可以增加多少公斤的重量仍然是“最佳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!')

1 个答案:

答案 0 :(得分:1)

这非常简单,与您对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是此人weightmaxweight之间差异的绝对值,舍入小数点后两位

希望这有帮助!