首页 > 解决方案 > 我将如何计算每个类别 BMI 计算器中的人数

问题描述

我需要做什么来计算每个类别中的每个人。体重过轻、正常体重、超重和肥胖?统计的每个类别中的个人数量以及显示的每个类别中的数量?

recipients = ["John", "Dee", "Aleister", "Lilith", "Paul", "Reggy"]
BMI_calc = []


def BMI(weights, heights):
    bmi_total = (weights * 703) / (heights ** 2)
    return bmi_total


def check(BMI):
  if BMI <= 18.5:
    print("Your underweight.")

  elif BMI > 18.5 and BMI < 24.9:
    print("You're normal weight.")

  elif BMI > 25 and BMI < 29.9:
    print("You're overweight.")

  elif BMI > 30:
    print("You're obese.")


for recipient in recipients:
    heights_ = int(input("What is your height " + recipient + "  :" ))
    weights_ = int(input("What is your weight " + recipient + "  :" ))
    BMI_info={"name":recipient,"weight":weights_,"height":heights_,"BMI":BMI(weights_, heights_)}
    BMI(BMI_info["weight"],BMI_info["height"])
    BMI_calc.append(BMI_info)



for person_info in BMI_calc:
    print(person_info["name"],end="\t")
    check(person_info["BMI"])

标签: python

解决方案


我遇到了和你一样的问题,这就是我遇到你的问题的方式。我没有按常规方式计算(例如体重不足 += 1),而是为每个 BMI 类别创建了列表。我只使用了 3 个类别

OVER_LIST = []
UNDER_LIST = []
NORMAL_LIST = []

我的函数看起来像这样:

def your_function_name():
    if BMI > 30:
        OVER_LIST.append(BMI)
    elif BMI < 10:
        UNDER_LIST.append(BMI)
    else:
        NORMAL_LIST.append(BMI)

最后,我在 for 循环的每次迭代中调用了该函数,并简单地打印了列表的长度:

for BMI in BMI_list:
    your_function_name()

print('Overweight = ',len(OVER_LIST),'\nUnderweight = ',len(UNDER_LIST),'\nNormal weight = ',len(NORMAL_LIST))

推荐阅读