本文主要是介绍Python+Excel数据分析实战:军事体能考核成绩评定(八)体型评定,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这一章我们实现男子和女子体型的评定,原始数据是身高、体重或者体脂百分比。作为军人,达到体型合格是基本要求,也是军人形象气质的基础,相关规定:体型合格就不用考虑单项不合格的情况,只看通用训练科目成绩总分;体型不合格,就不能再有单项不及格的情况,若有单项不及格,不管总分多少,直接总评不合格。
一、基本情况
BMI指数(Body Mass Index,简称BMI),身体质量指数,是用体重千克数除以身高米数的平方得出的数字,是国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。当我们需要比较及分析一个人体的体重对于不同高度的人所带来的健康影响时,BMI值是一个中立而可靠的指标。
计算公式为:BMI=体重(千克)除以身高(米)的平方。
通过分析下表,只要对Excel表里的原始身高、体重值进行计算,看BMI值是否满足下表BMI的范围的要求,就能得知体型是否合格。若BMI指数不合格,再进一步采用体脂百分比评定。
所以录入Excel表中的标准只需要BMI、PBF的范围大小值即可,且各年龄段的最小值是相同的:
同样,通过Python的openpyxl模块读取标准表中的数据,得到BMI、PBF的范围大小值即可。
二、代码实现
1.男子和女子体型计算写成了一个带性别参数的类,文件名为bodily_form.py,根据性别参数,读取工作簿上不同区域的标准数据。方法bodilyform_score_computing()有3个参数,年龄、BMI指数、体脂百分比,返回“合格”或者“不合格”。
# 男子女子体型的计算
# 从工作表“体型标准”中读取数据import openpyxlclass BodilyForm:def __init__(self, gender):self.gender = gender # 性别wb = openpyxl.load_workbook('通用训练课目考核成绩计算.xlsx')body_type = wb['体型标准']if self.gender == '男':self.lowest_BMI = body_type['A3'].value # 最低身体质量指数self.age24_BMI = body_type['B3'].value # 24岁以下,最高身体质量指数self.age25_29_BMI = body_type['C3'].value # 25~29岁,最高身体质量指数self.age30_39_BMI = body_type['D3'].valueself.age40_49_BMI = body_type['E3'].valueself.age50_59_BMI = body_type['F3'].value# print(self.lowest_BMI,self.age24_BMI,self.age25_29_BMI,self.age30_39_BMI,self.age40_49_BMI,# self.age50_59_BMI)self.lowest_PBF = body_type['A11'].value # 最低体脂比self.age24_PBF = body_type['B11'].value # 24岁以下,最高体脂比self.age25_29_PBF = body_type['C11'].value # 25~29岁,最高体脂比self.age30_39_PBF = body_type['D11'].valueself.age40_49_PBF = body_type['E11'].valueself.age50_59_PBF = body_type['F11'].value# print(self.lowest_PBF, self.age24_PBF, self.age25_29_PBF, self.age30_39_PBF, self.age40_49_PBF,# self.age50_59_PBF)elif self.gender == '女':self.lowest_BMI = body_type['A7'].value # 最低身体质量指数self.age24_BMI = body_type['B7'].value # 24岁以下,最高身体质量指数self.age25_29_BMI = body_type['C7'].value # 25~29岁,最高身体质量指数self.age30_39_BMI = body_type['D7'].valueself.age40_49_BMI = body_type['E7'].valueself.age50_59_BMI = body_type['F7'].value# print(self.lowest_BMI, self.age24_BMI, self.age25_29_BMI, self.age30_39_BMI, self.age40_49_BMI,# self.age50_59_BMI)self.lowest_PBF = body_type['A15'].value # 最低体脂比self.age24_PBF = body_type['B15'].value # 24岁以下,最高体脂比self.age25_29_PBF = body_type['C15'].value # 25~29岁,最高体脂比self.age30_39_PBF = body_type['D15'].valueself.age40_49_PBF = body_type['E15'].valueself.age50_59_PBF = body_type['F15'].value# print(self.lowest_PBF, self.age24_PBF, self.age25_29_PBF, self.age30_39_PBF, self.age40_49_PBF,# self.age50_59_PBF)def bodilyform_score_computing(self,age,body_mass_index,pbf):'''根据身体质量指数、体脂比计算体型是否合格'''if age <= 24:if self.lowest_BMI <= body_mass_index <= self.age24_BMI:return '合格'elif pbf == None:return '不合格'elif self.lowest_PBF <= pbf <= self.age24_PBF:return '合格'else:return '不合格'elif 25 <= age <= 29:if self.lowest_BMI <= body_mass_index <= self.age25_29_BMI:return '合格'elif pbf == None:return '不合格'elif self.lowest_PBF <= pbf <= self.age25_29_PBF:return '合格'else:return '不合格'elif 30 <= age <= 39:if self.lowest_BMI <= body_mass_index <= self.age30_39_BMI:return '合格'elif pbf == None:return '不合格'elif self.lowest_PBF <= pbf <= self.age30_30_PBF:return '合格'else:return '不合格'elif 40 <= age <= 49:if self.lowest_BMI <= body_mass_index <= self.age40_49_BMI:return '合格'elif pbf == None:return '不合格'elif self.lowest_PBF <= pbf <= self.age40_49_PBF:return '合格'else:return '不合格'elif 50 <= age <= 59:if self.lowest_BMI <= body_mass_index <= self.age50_59_BMI:return '合格'elif pbf == None:return '不合格'elif self.lowest_PBF <= pbf <= self.age50_59_PBF:return '合格'else:return '不合格'if __name__ == "__main__": # 测试本模块bodily_form = BodilyForm('男')result = bodily_form.bodilyform_score_computing(50,29.5,0.243)print(result)
2.对主程序training_performance.py进行修改。根据性别,进行不同的实例化;判断性别后,先计算单杠成绩,接着计算仰卧起坐成绩,再计算蛇形跑成绩、3公里成绩,最后计算体型,分别写入表中换算成绩的相应位置。
import openpyxl
import datetime as dt
from age_computing import age_computing # 导入年龄计算模块
from pullup_male import Pullup_standard_data # 导入男子引体向上成绩计算模块
from flex_arm_hang_female import Flex_arm_hang_standard_data # 导入女子单杠曲臂悬垂的成绩计算模块
from situp_male import Situp_male_standard_data # 导入男子仰卧起坐成绩计算模块
from situp_female import Situp_female_standard_data # 导入女子仰卧起坐成绩计算模块
from serpentine_run_male import Serpentine_run_male_standard_data # 导入男子蛇形跑成绩计算模块
from serpentine_run_female import Serpentine_run_female_standard_data # 导入女子蛇形跑成绩计算模块
from highland2001up_3km_male import Highland2001up3kmMaleStandardData # 导入男子高原2001~3000,3000米成绩计算模块
from highland3001up_3km_male import Highland3001up3kmMaleStandardData # 导入男子高原3001~4000,3000米成绩计算模块
from highland2001up_3km_female import Highland2001up3kmFemaleStandardData # 导入女子高原2001~3000,3000米成绩计算模块
from highland3001up_3km_female import Highland3001up3kmFemaleStandardData # 导入女子高原2001~3000,3000米成绩计算模块
from flatland_3km import Flatland3kmStandardData # 导入男女平原3000米成绩计算模块
from bodily_form import BodilyForm # 导入男女体型计算模块wb=openpyxl.load_workbook('通用训练课目考核成绩计算.xlsx')
ws_training_score = wb['体能考核成绩']
ws_personnel_parameters = wb['人员参数设置']col_parameters = [c.value for c in list(ws_personnel_parameters.columns)[1][1:]]
staff_type = col_parameters[0] #人员类别
elevation = col_parameters[1] #地区海拔if staff_type == '三类':lowest_score = 55
if staff_type == '二类':lowest_score = 60
if staff_type == '一类':lowest_score = 65pullup_sd = Pullup_standard_data() # 实例化男子引体向上成绩计算
flexarmhang_sd = Flex_arm_hang_standard_data() # 实例化女子单杠曲臂悬垂成绩计算
situp_male = Situp_male_standard_data() # 实例化男子仰卧起坐成绩计算
situp_female = Situp_female_standard_data() # 实例化男子仰卧起坐成绩计算
serpentine_run_male = Serpentine_run_male_standard_data() # 实例化男子蛇形跑成绩计算
serpentine_run_female = Serpentine_run_female_standard_data() # 实例化女子蛇形跑成绩计算
bodily_form_male = BodilyForm('男') # 实例化男子体型成绩计算
bodily_form_female = BodilyForm('女') # 实例化女子体型成绩计算if elevation in range(0,2001):long_distance_run_male = Flatland3kmStandardData('男') # 实例化男子平原3Km米跑计算模块long_distance_run_female = Flatland3kmStandardData('女') # 实例化女子平原3Km米跑计算模块
elif elevation in range(2001,3001):long_distance_run_male = Highland2001up3kmMaleStandardData(elevation) # 实例化男子高原海拔2001~3000米,3Km米跑计算模块long_distance_run_female = Highland2001up3kmFemaleStandardData(elevation) # 实例化女子高原海拔2001~3000米,3Km米跑计算模块
elif elevation in range(3001,4001):long_distance_run_male = Highland3001up3kmMaleStandardData(elevation) # 实例化男子高原海拔3001~4000米,3Km米跑计算模块long_distance_run_female = Highland3001up3kmFemaleStandardData(elevation) # 实例化女子高原海拔3001~4000米,3Km米跑计算模块rngs = ws_training_score.iter_rows(min_row=6)
for row in rngs:gender = row[4].value # 性别pullup = row[11].value # 单杠原始数量situp = row[7].value # 仰卧起坐原始数量serpentine_run = row[9].value # 蛇形跑原始数量long_distance_run = row[13].value # 3Km跑原始时长height = row[16].value # 身高weight = row[17].value # 体重percent_body_fat = row[18].value # 体脂百分比if row[5].value:#print(row[5].value)age = age_computing(row[5].value) #由出生日期计算年龄,精确到天row[6].value = ageif gender == '男':if row[6].value != None: # 年龄不能为空if pullup != None: # 单杆原始数不能为空score_pullup = pullup_sd.pullup_score_computing(age, pullup) # 计算男子单杠或俯卧撑成绩#print(row[0].value,pullup,row[12].value) # 测试if not score_pullup < lowest_score:row[12].value = score_pullupelse:row[12].value = 0if situp != None: # 仰卧起坐原始数不能为空score_situp = situp_male.situp_male_score_computing(age, situp) # 计算男子仰卧起坐成绩#print(row[0].value, situp, row[8].value)if not score_situp < lowest_score:row[8].value = score_situpelse:row[8].value = 0if serpentine_run != None: # 蛇形跑原始数不能为空score_serpentine_run = serpentine_run_male.serpentine_run_male_score_computing(age, serpentine_run) # 计算男子蛇形跑成绩#print(row[0].value, serpentine_run, row[10].value)if not score_serpentine_run < lowest_score:row[10].value = score_serpentine_runelse:row[10].value = 0if long_distance_run != None: # 3Km跑原始时长不能为空score_long_run = long_distance_run_male.longrun_score_computing(age, long_distance_run) # 计算男子3Km跑成绩#print(row[0].value, long_distance_run, row[14].value)if not score_long_run < lowest_score:row[14].value = score_long_runelse:row[14].value = 0if height !=None and weight !=None: #身高体重不为空时body_mass_index = weight / (height * height)row[19].value = bodily_form_male.bodilyform_score_computing(age,body_mass_index,percent_body_fat)elif gender == '女':if row[6].value != None:if pullup != None: # 单杆原始数不能为空score_pullup = flexarmhang_sd.flex_arm_hang_score_computing(age, pullup) # 女子曲臂悬垂或俯卧撑成绩计算#print(row[0].value,pullup, row[12].value)if not score_pullup < lowest_score:row[12].value = score_pullupelse:row[12].value = 0if situp != None: # 仰卧起坐原始数不能为空score_situp = situp_female.situp_female_score_computing(age, situp) # 计算女子仰卧起坐成绩#print(row[0].value, situp, row[8].value)if not score_situp < lowest_score:row[8].value = score_situpelse:row[8].value = 0if serpentine_run != None: # 蛇形跑原始数不能为空score_serpentine_run = serpentine_run_female.serpentine_run_female_score_computing(age, serpentine_run) # 计算女子蛇形跑成绩#print(row[0].value, serpentine_run, row[10].value)if not score_serpentine_run < lowest_score:row[10].value = score_serpentine_runelse:row[10].value = 0if long_distance_run != None: # 3Km跑原始时长不能为空score_long_run = long_distance_run_female.longrun_score_computing(age, long_distance_run) # 计算女子3Km跑成绩#print(row[0].value, long_distance_run, row[14].value)if not score_long_run < lowest_score:row[14].value = score_long_runelse:row[14].value = 0if height !=None and weight !=None: #身高体重不为空时body_mass_index = weight / (height * height)row[19].value = bodily_form_female.bodilyform_score_computing(age,body_mass_index,percent_body_fat)else: # 性别输入有误print('序号%d %s 的性别填写错误'%(row[0].value,row[1].value))wb.save('计算结果.xlsx')
print('计算已完成!')
运行后,生成文件“计算结果.xlsx”如下:
到此,我们成功实现了体型计算的功能。
军事体能考核成绩评定系统下载
军事体能考核成绩评定系统全套Python源码下载
这篇关于Python+Excel数据分析实战:军事体能考核成绩评定(八)体型评定的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!