按图施工,设计下述BMI类,用于计算身体质量指数。该指数等于体重(kg)除以身高(米)的平方。
![](https://img-blog.csdnimg.cn/img_convert/d112534ed63e4286bcd14532ce2c6d82.png)
相应的判定标准如下图所示。
![](https://img-blog.csdnimg.cn/img_convert/fdbada81a854429caf5e6a4ca98cc141.png)
BMI类接口定义:
classBMI: ...
其中,name为姓名,age为年龄,height为身高,单位为米,weight为体重,单位为kg。
getBMI()函数返应计算所得的身体质量指数;
getStatus()返回评价结果,其值应为超轻/标准/超重/肥胖之一。
裁判测试程序样例:
sName = input() #输入姓名iAge = int(input()) #输入年龄fHeight = eval(input()) #输入身高,预期为浮点数,单位米fWeight = eval(input()) #输入体重,预期为浮点数,单位千克bmi=BMI(sName,iAge,fHeight,fWeight) #实例化BMI类print("Name:",bmi.name)
print("Age:",bmi.age)
print("BMI = %.3f" % bmi.getBMI())
print("Result =",bmi.getStatus())
输入样例:
Alex
27
1.75
68
输出样例:
Name: Alex
Age: 27
BMI = 22.204
Result = 标准
其中答案之一:
class BMI: #创建BMI类接口
def __init__(self,sName,iAge,fHeight,fWeight):
self.name=sName
self.age=iAge
self.height=fHeight
self.weight=fWeight
def getStatus(self):
b=fWeight/(fHeight*fHeight)
if b<18:
return '超轻'
elif 25>b>=18:
return '标准'
elif 25<=b<27:
return '超重'
else:
return '肥胖'
def getBMI(self):
b=fWeight/(fHeight*fHeight)
return b
执行结果:
![](https://img-blog.csdnimg.cn/img_convert/7b07f780c15a4b5fb5127cc76eae7972.png)