首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

30分请问个python小程序的执行异常

2012-05-21 
30分请教个python小程序的执行错误Python codeclass ShortInputException(Exception):def __int__(self, l

30分请教个python小程序的执行错误

Python code
class ShortInputException(Exception):    def __int__(self, length, atleast):        Exception.__int__(self)        self.length = length        self.atleast = atleastl = 5try:    s = raw_input('Enter something: ')    if len(s) < l:        raise ShortInputException(len(s), l)except EOFError:    print 'EOF error, Please check ur input'except ShortInputException, x:    print 'The input length is %d, should atleast be %d' % (x.length, x.atleast)else:    print 'No exception was raised'


------------
上面是源码, 在IDE中运行出现如下错误:
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
  exec codeObject in __main__.__dict__
  File "D:\Python\raise except.py", line 16, in <module>
  print 'The input length is %d, should atleast be %d' % (x.length, x.atleast)
AttributeError: 'ShortInputException' object has no attribute 'length'

大意就是x没有length属性, 程序是从python简明教程里基本扒下来的,
请教代码哪里有问题?

[解决办法]
汗啊!想来想去还以为是Python的bug

你实际写的:
Python code
    def __int__(self, length, atleast):
[解决办法]
晕,发现的比你慢了。呵呵,刚打开就去吃东西了,嘿嘿。

热点排行