|
发表于 2022-3-8 00:15:07
|
显示全部楼层
- class WeightError(Exception):
- def __init__(self, high, athigh, cehigh):
- self.high = high
- self.athigh =athigh
- self.cehigh =cehigh
- if __name__=='__main__':
- try:
- weight = int(input('请输入体重 单位(斤):'))
- height = int(input('请输入身高 单位(CM):'))
- if height<30 or height>250:
- # 引发异常
- raise WeightError (height , 30, 250)
- except WeightError as result:
- print('你输入的身高为%s,身高请输入在%s到%s范围。'%(result.high, result.athigh ,result.cehigh))
- else:
- bz_weight = height - 100
- cz = ((weight-bz_weight*2)/weight)
- if -0.05 <=cz<= 0.05:
- print('恭喜您,体重正常达标!')
- elif cz>0.05:
- print('很遗憾,你的体重超标,请及时锻炼!')
- elif cz<-0.05:
- print('抱歉,你的体重不达标!')
复制代码
错误:
1、类定义,赋值
2、错误引用,已经对象化的错误对象WeightError,不能用self
|
|