python - 我将如何计算每个类别 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"])
解决方案
我遇到了和你一样的问题,这就是我遇到你的问题的方式。我没有按常规方式计算(例如体重不足 += 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))
推荐阅读
- python - 无法使用 Python 代码访问 NSF 文件 - “注释错误:无法打开 ID 文件”
- android - 无法将元素添加到我从 firebase 获得的 Arraylist
- ios - 我无法在 iOS 14 中为具有 UIDatePickerStyle.inline 的 UIDatePicker 更改文本颜色
- azure-devops - 在 Azure 管道之间传递变量
- python-3.x - 连接来自两个不同来源的两个数据帧。如果数据框丢失,则无法处理情况
- tensorflow - 以最大验证准确度停止训练。这是一个好习惯吗?
- typescript - typescript的Int8ArrayConstructor类型定义可以简化吗
- javascript - Input values not resetting - vuejs
- java - 如何使用 Java 中的另一个对象创建一个对象?
- r - RStudio 中的 SparkR:sparkR.session:JVM 在 10 秒后未准备好