1.新建myindexerror模块,命名为myindexerror.py
class MyIndexError:
def __init__ (self,value):
self.value=value
2.新建myvalueerror模块,命名为myvalueerror.py
class MyValueError:
def __init__ (self,value):
self.value=value
3.新建myerror.py文件
import myindexerror
import myvalueerror
userList=['maxianglin','wanglili','malingling','fanxiaoxuan']
user_str=''
user_name=''
input_selectIndex=0
user_name=0
try:
input_selectIndex=int(raw_input('请输入要查询的用户名编号:'))
user_str=userList[input_selectIndex]
input_selectName=raw_input('请输入要查询的用户名:')
user_name=userList.index(input_selectName)
except IndexError,e:
print '出现的错误信息编号为:',myindexerror.MyIndexError('1').value
except ValueError,e:
print '出现的错误信息编号为:',myvalueerror.MyValueError('2').value
else:
print '您输入的编号为'+str(input_selectIndex)+'的用户为:'+user_str
print '您输入的用户名'+input_selectName+'在列表中对应的索引为:'+str(user_name)
4.运行结果:
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
请输入要查询的用户名编号:4
出现的错误信息编号为: 1
>>> ================================ RESTART ================================
>>>
请输入要查询的用户名编号:2
请输入要查询的用户名:yinguopeng
出现的错误信息编号为: 2
>>> ================================ RESTART ================================
>>>
请输入要查询的用户名编号:2
请输入要查询的用户名:maxianglin
您输入的编号为2的用户为:malingling
您输入的用户名maxianglin在列表中对应的索引为:0
>>>