NIEJIANWEI7 2010-06-25 13:56
浏览 295
已采纳

帮忙看看,哪里出错了

class shortException(Exception):
def int(self,length,atleast):
Exception.__int__(self)
self.length=length
self.atleast=atleast
try:
b=3
a =raw_input('ple input a word>>')
if len(a)<b:
raise shortException(len(a),b)
except EOFError:
print 'don\'t input EOFError'
except shortException,x:
print 'this number length is %d and aeleast is %d'%(x.length,x.atleast)
except AttributeError:
print'wokao'
else:
print 'the length is %s'%a

a byte of python里面的一个例子,为什么老是提示没有length属性
异常信息:AttributeError: 'shortException' object has no attribute 'length'

能否解答下···,详细点,TKS

  • 写回答

1条回答 默认 最新

  • u010804832 2010-06-25 14:38
    关注

    Exception类比较特殊,不是直接用属性来取值的,具体看文档
    [url]http://docs.python.org/tutorial/errors.html[/url]
    [code="python"]
    class shortException(Exception):
    def int(self,length,atleast):
    Exception.__int__(self)
    self.length=length
    self.atleast=atleast
    try:
    b=3
    a =raw_input('ple input a word>>')
    if len(a)<b:
    raise shortException(len(a),b)
    except EOFError:
    print 'don\'t input EOFError'
    except shortException,x:
    # print 'this number length is %d and aeleast is %d'%(x.length,x.atleast)
    length, atleast = x.args
    print 'this number length is %d and aeleast is %d'%(length,atleast)
    except AttributeError:
    print'wokao'
    else:
    print 'the length is %s'%a
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用