簡體   English   中英

BMI計算器錯誤

[英]Error in BMI calculator

我正在嘗試使用python中的類進行計算器。 我試圖以這種方式解決:

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

當我調用構造函數: p = Person("hari", "25", "6", "30")p.get_bmi_result它返回<bound method Person.get_bmi_result of <Person object at 0x00DAEED0>> 我以公斤為單位輸入重量,以英尺為單位輸入身高,在計算中,我嘗試將英尺轉換為厘米。

您只是忘了調用您的方法:

p.get_bmi_result()

注意那些()括號。 您僅取消引用了綁定方法對象。

通過調用,您的代碼可以正常運行:

>>> 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'

顯然,您的公式仍需要調整,即使體重僅30英寸(?)小,體重為6磅的人的體重指數(BMI)0.000007 根本不足。

根據體重和身高單位的大小,可能需要參考BMI公式並稍微調整一下方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM