Python中的BMI索引

时间:2016-12-26 15:11:51

标签: python

我需要帮助做我的作业代码,我有一种感觉我可能会遗漏一些东西,你的帮助/反馈将不胜感激!

此致 梅雷迪思

def calc_BMI():
   weight=requestNumber("Enter weight (kg)")
   height=requestNumber("Enter Height (meters)")
   bmi=(weight/(height*height))
   print 'Your BMI in %2f' % bmi
   if bmi=<15
      print('Your weight status is Very Severely Underweight')    
   elif bmi>=15.0 and bmi<=16.0
    print ('Your weight status is Severely Underweight')
   elif bmi>=16.0 and bmi<=18.5
    print ('Your weight status is Underweight')
   elif bmi>= 18.5 and bmi <=25       
    print('Your weight staus is Normal')
   elif bmi >=25 and bmi <=30
    print ('Your weight status is Overweight')
   elif bmi>=30 and bmi <=35
    print ('Your weight status is Moderately Obese')
   elif bmi >=35 and bmi<=40
    print ('Your weight status is Severely Obese')
   elif bmi <=40 
    print ('Your weight status is Very Severely Obese') 

2 个答案:

答案 0 :(得分:0)

您的代码有多个问题:

将它们列在这里:

  1. requestNumber不是python中的方法。导入具有此方法的库。或使用默认的input方法。
  2. weight = requestNumber("Enter weight (kg)")

    instead

    weight = input("Enter weight (kg)")

    1. 在结束时你应该有一个':'
    2. 例如:if bmi <= 15:

      if bmi=<15也是错误的。它应该是if bmi <= 15:

      1. 从您的问题中删除输入代码部分。

        elif bmi >=35 and bmi<=40 enter code here

      2. 代码中的最后一行但是1行的错误。它应该是&gt; =

        elif bmi <=40

        print ('Your weight status is Very Severely Obese')

      3. 工作代码:

        def calc_BMI():
            weight = input("Enter weight (kg)")
            height = input("Enter Height (meters)")
            bmi = (int(weight)/(float(height)**2))
            print('Your BMI in %2f' % bmi)
            if bmi <= 15:
                print('Your weight status is Very Severely Underweight')    
            elif bmi >= 15.0 and bmi <= 16.0:
                print ('Your weight status is Severely Underweight')
            elif bmi >= 16.0 and bmi<= 18.5:
                print ('Your weight status is Underweight')
            elif bmi >= 18.5 and bmi <= 25:      
                print('Your weight staus is Normal')
            elif bmi >= 25 and bmi <= 30:
                print('Your weight status is Overweight')
            elif bmi >= 30 and bmi <= 35:
                print('Your weight status is Moderately Obese')
            elif bmi >= 35 and bmi <= 40:
                print('Your weight status is Severely Obese')
            elif bmi >= 40:
                print('Your weight status is Very Severely Obese')
        

        输出:

        calc_BMI()
        
        Enter weight (kg)78
        Enter Height (meters)1.8
        Your BMI in 24.074074
        Your weight staus is Normal
        

        有用的说明:

        1. 尝试遵循编码惯例,例如pep-8 [Link]

        2. 正确缩进代码。看看我如何在'&gt; ='之前和之后添加空格。这也是pep-89惯例的一部分。

          例如:如果重量> = 40:

答案 1 :(得分:0)

您的代码中存在多个错误,我已对这些问题进行了评论:

def calc_BMI():
   weight=int(input("Enter weight (kg)"))#make sure value taken is int
   height=float(input("Enter Height (meters)"))#make sure value taken is float
   bmi=(weight/(height**2))#use the power operator for squaring instead
   print ('Your BMI in %2f' % bmi)
   if bmi <= 15 :#colon eexpected
         print('Your weight status is Very Severely Underweight')
   elif bmi>=15.0 and bmi<=16.0:#colon eexpected
        print ('Your weight status is Severely Underweight')
   elif bmi>=16.0 and bmi<=18.5:#colon eexpected
        print ('Your weight status is Underweight')
   elif bmi>= 18.5 and bmi <=25 :#colon eexpected
        print('Your weight staus is Normal')
   elif bmi >=25 and bmi <=30:#colon eexpected
        print ('Your weight status is Overweight')
   elif bmi>=30 and bmi <=35:#colon eexpected
        print ('Your weight status is Moderately Obese')
   elif bmi >=35 and bmi<=40:#colon eexpected
        print ('Your weight status is Severely Obese')
   else:#no need for a elif,rather use else for the rest
       print('Your weight status is Very Severely Obese')
calc_BMI()# call the function to run