简体   繁体   English

BMI计算器错误

[英]Error in BMI calculator

I am trying to calculator using classes in python. 我正在尝试使用python中的类进行计算器。 I tried to solve in this way: 我试图以这种方式解决:

class Person(object):
  def __init__(self,name,age,weight,height):
      self.name = name
      self.age = age
      self.weight = float(weight)
      self.height = float(height)
  def get_bmi_result(self):
      bmi = (self.weight)/float((self.height*30.48)*(self.height*30.48))
      print bmi
      if bmi <= 18.5:
        return "underweight"
      elif bmi>18.5 and bmi<25:
           return "normal"
      elif bmi>30:
           return "obese"
  pass

when I call the constructor: p = Person("hari", "25", "6", "30") and p.get_bmi_result it was returning <bound method Person.get_bmi_result of <Person object at 0x00DAEED0>> . 当我调用构造函数: p = Person("hari", "25", "6", "30")p.get_bmi_result它返回<bound method Person.get_bmi_result of <Person object at 0x00DAEED0>> I entered weight in kilograms and height in foots and in the calculation I tried to convert foot to centimeters. 我以公斤为单位输入重量,以英尺为单位输入身高,在计算中,我尝试将英尺转换为厘米。

You simply forgot to call your method: 您只是忘了调用您的方法:

p.get_bmi_result()

Note those () parenthesis. 注意那些()括号。 You only dereferenced the bound method object. 您仅取消引用了绑定方法对象。

With the call, your code runs just fine: 通过调用,您的代码可以正常运行:

>>> class Person(object):
...   def __init__(self,name,age,weight,height):
...       self.name = name
...       self.age = age
...       self.weight = float(weight)
...       self.height = float(height)
...   def get_bmi_result(self):
...       bmi = (self.weight)/float((self.height*30.48)*(self.height*30.48))
...       print bmi
...       if bmi <= 18.5:
...         return "underweight"
...       elif bmi>18.5 and bmi<25:
...            return "normal"
...       elif bmi>30:
...            return "obese"
... 
>>> p = Person("hari", "25", "6", "30")
>>> p.get_bmi_result()
7.17594027781e-06
'underweight'

Clearly your formula needs adjusting still, a BMI of 0.000007 is radically underweight for someone weighing 6 stone, even if only 30 inches(?) small. 显然,您的公式仍需要调整,即使体重仅30英寸(?)小,体重为6磅的人的体重指数(BMI)0.000007 根本不足。

Depending on what your weight and height unit sizes are, you may need to consult the BMI formula and adjust your method a little. 根据体重和身高单位的大小,可能需要参考BMI公式并稍微调整一下方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM