实验7

 1 class User:
 2     def __init__(self, name='guest', password=111111, status=1):
 3         self._name = name
 4         self._password = password
 5         self._status = status
 6 
 7     def info(self):
 8         print('账户信息:')
 9         if self._status == 1:
10             print(f'用户名:{self._name},密码:{self._password},账户正常')
11         else:
12             print(f'用户名:{self._name},密码:{self._password},账户被封禁')
13 
14     def modify_password(self):
15         n = 1
16         while n <= 3:
17             old_num = eval(input('请输入旧密码:'))
18             if old_num == self._password:
19                 new_num = eval(input('请输入新密码:'))
20                 self._password = new_num
21                 print('密码修改成功')
22                 break
23             else:
24                 n += 1
25                 if n == 3:
26                     print('账户已锁,请稍后再试')
27                     self._status = 0
28                     break
29                 else:
30                     print('密码错误,请重新输入:')
31 
32 
33 class Admin(User):
34     def __init__(self, name='admin', password=999999, status=1):
35         super().__init__(name, password, status)
36 
37     def info(self):
38         User.info(self)
39 
40     def reset_password(self):
41         User.modify_password(self)
42 
43     def ban_user(self):
44         User._status = 0
45         print('账户已封禁')
46 
47     def unblock_user(self):
48         User._status = 1
49         print('账户已解封')
50 
51 
52 def main():
53     u1 = User()
54     u1.info()
55     u1.modify_password()
56     u1.info()
57     a1 = Admin()
58     a1.info()
59     a1.reset_password()
60     a1.ban_user()
61     a1.unblock_user()
62 
63 
64 if __name__ == '__main__':
65     main()
 1 from user import User, Admin
 2 
 3 u1 = User('site', 123456, 1)
 4 u1.info()
 5 u1.modify_password()
 6 u1.info()
 7 
 8 u2 = Admin('Jo', 654321, 1)
 9 u2.info()
10 u2.reset_password()
11 u2.ban_user()
12 u1.info()
13 u2.unblock_user()
14 u1.info()

 

posted on 2022-06-06 22:39  张玮珊  阅读(29)  评论(3编辑  收藏  举报