|
发表于 2021-9-7 14:25:39
|
显示全部楼层
- class UserNameException(Exception):
- def __init__(self, count):
- self.count = count
- def showError(self):
- if self.count:
- print(f"用户名不存在,还有{self.count}次机会,请重新登录")
- else:
- print("机会用完,账号锁定")
- class PasswordsException(Exception):
- def __init__(self, count):
- self.count = count
- def showError(self):
- if self.count:
- print(f"密码错误登录失败,还有{self.count}次机会,请重新登录")
- else:
- print("机会用完,账号锁定")
- def main():
- users = {'Jack': 2586, 'Tom': 575, 'Peter': 106, 'Steve': 9043, 'Paul': 19482, 'Bill': 48637}
- count = 3
- while count:
- count -= 1
- try:
- name = input("请输入用户名:")
- password = int(input("请输入用户密码:"))
- if not(name in users.keys()):
- raise UserNameException(count)
- elif users[name] != password:
- raise PasswordsException(count)
- else:
- print("登录成功")
- break
- except UserNameException as e:
- e.showError()
- continue
- except PasswordsException:
- e.showError()
- continue
- if __name__ == "__main__":
- main()
复制代码 |
|