Python中的BMI指数

2024-09-28 22:33:04 发布

您现在位置: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') 

Tags: andyourisstatusprintbmienterheight
2条回答

您的代码中有几个错误,我已对这些问题进行了注释:

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 

您的代码有多个问题:

将在此处列出:

  1. requestNumber不是python中的方法。或者导入具有此方法的库。或者使用默认的input方法。你知道吗

weight = requestNumber("Enter weight (kg)")

instead

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

  1. 如果条件结束,你应该有一个':'

例如:if bmi <= 15:

而且if bmi=<15是错误的。应该是if bmi <= 15:

  1. 从问题中删除“在此处输入代码”部分。你知道吗

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

  2. 最后一行代码中的Bug。应该是>;=

    elif bmi <=40

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

工作代码:

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. 正确缩进代码。查看如何在“>;=”前后添加空格。这也是pep-89公约的一部分。你知道吗

    例如:如果重量大于等于40:

相关问题 更多 >