我们写入一个判断表
def input_id():
#请输入学号
id = input("请输入学号:")
#假设 ID为5的已经存在
if id == "5":(id可以换其他的)
e= Exception("ID学号已经存在")#创建一个异常对象
raise e ##抛出异常
else:
return id
id= ""
try:
id = input_id()
except Exception as e:
print("ID有问题:%s"%e)
id = input_id()
print("ID的值:%s"%id)
这里我们先假如要输出id = 5的话
如果是输出5以外的id则直接输出: