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

Posted

技术标签:

【中文标题】寻找解决方案的绝对初学者【英文标题】:Absolute beginner looking for solution 【发布时间】:2017-08-10 00:36:33 【问题描述】:

我想添加一行来计算您可以增加多少公斤的体重以保持“最佳 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 公式有关,而不是编程。您将如何手动计算此值? @Chris 这正是我所害怕的。缺乏数学知识可能是我的问题。 BMI 公式非常简单:BMI = 体重/(身高^2)。问题是体重可以改变多少(因为身高几乎是恒定的)仍然在 18.5 - 24.9 BMI 范围内。 您已经弄清楚了一件重要的事情:“要仍然在 18.5 到 24.9 的 BMI 范围内”。所以这仅适用于该人 in 那个括号。 (您可能也希望将它用于体重过轻的用户。)确保将您的“仍然可以穿上另一个”信息放在正确的位置。将 BMI 设置为可接受的最大值,插入用户的身高,然后求解体重。 @Chris 非常感谢,搞定了! 【参考方案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之差的绝对值,保留2位小数

希望这会有所帮助!

【讨论】:

太棒了!我已经设法自己弄清楚了。我使用了相同的逻辑,但 float(round... 给了我一些麻烦。round(abs) 对我来说是新事物。无论如何,感谢您的回复,您真的让我有了正确的想法。 @ffs_remi 很高兴我能帮上忙!

以上是关于寻找解决方案的绝对初学者的主要内容,如果未能解决你的问题,请参考以下文章

J2EE web项目中解决所有路径问题

如何找到具有一组整数的函数的“最大绝对和”

从数组中找到元素的最小绝对和

Web 制作:外部 JS 文件中的绝对 URL?

将图标浮动在文本框上而不使用绝对位置 [重复]

SQL数据库设计初学者指南[关闭]