题目:
按图施工,设计下述BMI类,用于计算身体质量指数。该指数等于体重(kg)除以身高(米)的平方。
相应的判定标准如下图所示。
BMI类接口定义:
class BMI: ...
其中,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:
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<27:
return '超重'
elif 18<=b<25:
return '标准'
else:
return '肥胖'
def getBMI(self):
b=fWeight/(fHeight*fHeight)
return b