|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class WeightError(Exception):
- def __init__(self, high, athigh, cehigh):
- self.high == high
- self.athigh == athigh
- self.cehigh == cehigh
- 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范围。'%(self.high, self.athigh ,self.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('抱歉,你的体重不达标!')
复制代码
变量名有点乱
出现的问题是 AttributeError: 'WeightError' object has no attribute 'high'
不知道如何解决 不太理解 希望有帮忙解释下报错原因
谢谢拉
- 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
|
|