bithcc 2021-06-22 14:44 采纳率: 0%
浏览 15
已结题

关于python中定义异常过程中出现的的小问题

使用的是pycharm,python版本3.9,想要定义一个输入长度不足的异常。

代码如下:

class ShortInputException(Exception):
    def _int_(self,length,atleast):
        self.length=length
        self.atleast=atleast
try:
    text=input('please enter the password:')
    if len(text)<3:
        raise ShortInputException(len(text),3)
except EOFError:
    print('you have entered an end sign')
except ShortInputException as result:
    print('ShortInputException:you entered:%d,the len must be at least:%d'%(result.length,result.atleast))
else:
    print('no error happened')

错误如下:

please enter the password:1
Traceback (most recent call last):
  File "/home/hcc/PycharmProjects/pythonProject/test2.py", line 8, in <module>
    raise ShortInputException(len(text),3)
__main__.ShortInputException: (1, 3)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hcc/PycharmProjects/pythonProject/test2.py", line 12, in <module>
    print('ShortInputException:you entered:%d,the len must be at least:%d'%(result.length,result.atleast))
AttributeError: 'ShortInputException' object has no attribute 'length'

  • 写回答

1条回答 默认 最新

  • CSDN专家-黄老师 2021-06-22 14:59
    关注

    你debug调试发现,result是以元组格式表示的,如图

    如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢
     

    评论

报告相同问题?

悬赏问题

  • ¥15 umi接入sentry遇到问题
  • ¥15 HBuilderX打包H5网页,扫码模块无法使用
  • ¥15 Javascript跳转页面后,无法执行后面代码,如何解决?
  • ¥15 echarts绘制图表
  • ¥15 请教两个关于高德地图定位不准的技术问题
  • ¥15 根据企业名称 对照两个文件 样本筛选/匹配
  • ¥15 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据
  • ¥60 微信小程序如何上传QQ聊天文件
  • ¥300 开发的系统遭到无良商家的破解,请问如何防止再次发生,并追回损失