永发信息网

学习python异常处理,rasie ShortInputException(len(s),3),invalid syntax错误

答案:2  悬赏:0  手机版
解决时间 2021-02-15 08:48
  • 提问者网友:爱唱彩虹
  • 2021-02-15 00:26
3.0版本提示这一句invalid syntax错误,该怎么写
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self,length,atleast):
Exception.__init__(self)
self.length=length
self.atleast=atleast
try:
s=input('Enter something--->')
if len(s)<3:
rasie ShortInputException(len(s),3)
#O the work cam continue as usual here
except EOFError:
print('\nWhy did you do an EOF on me?')
except ShortInputException as x:
print('ShortInputException:The input was of length%d,\
was expecting at least %d'%(x.length,x.atleast))
else:
print('No exception was raised.')
最佳答案
  • 五星知识达人网友:玩世
  • 2021-02-15 00:58
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
s = input('Enter something--->')
if len(s) < 3:
raise ShortInputException(len(s), 3)
#O the work cam continue as usual here
except EOFError:
print('\nWhy did you do an EOF on me?')
except ShortInputException as x:
print('ShortInputException:The input was of length%d,\
was expecting at least %d' % (x.length, x.atleast))
else:
print('No exception was raised.')

注意最后一个else的缩进
全部回答
  • 1楼网友:夜余生
  • 2021-02-15 01:46
搜一下:学习python异常处理,rasie ShortInputException(len(s),3),invalid syntax错误
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯