实验7 类和模块

2022/6/5 23:20:11

本文主要是介绍实验7 类和模块,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 1 class User:
 2     count = 0
 3 
 4     def __init__(self, name='guest', password=111111, status=1):
 5         User.count += 1
 6         self.name = name
 7         self.password = password
 8         self.status = status
 9 
10     def info(self):
11         if self.status == 1:
12             print(f'{self.name},{self.password} 账户正常')
13         else:
14             print(f'{self.name},{self.password} 账户被封禁')
15 
16     def modify_password(self):
17         i = 0
18         while i < 3:
19             n = input("请输入旧密码:")
20             if eval(n) == self.password:
21                 n = eval(input("输入新密码:"))
22                 print('密码修改成功!')
23                 self.password = n
24                 break
25             else:
26                 i += 1
27         else:
28             print("账户已锁住,稍后再试")
29             self.status = 0
30 
31 class Admin(User):
32 
33     def __init__(self, name='admin', password=999999, status=1):
34         super().__init__(name, password, status)
35 
36     def info(self):
37         print("管理者账户")
38         super().info()
39 
40     def ban_user(self, user):
41         user.status = 0
42 
43     def unblock_user(self, user):
44         user.status = 1
45 
46     def reset_password(self, user):
47         user.modify_password()
48 
49 
50 if __name__ == "__main__":
51     u1 = User()
52     u1.info()
53 
54     admin1 = Admin()
55     admin1.info()
56 
57     admin1.ban_user(u1)
58     u1.info()
59     admin1.unblock_user(u1)
60     u1.info()
61     admin1.reset_password(u1)

 



这篇关于实验7 类和模块的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程