实验7 类和模块

2022/6/1 23:22:05

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

 1 '''
 2 用户账户
 3 数据:用户名,密码
 4 操作:显示账户信息,修改密码
 5 '''
 6 
 7 class User:
 8     def __init__(self, name='guest', password='111111', status=1):
 9         self.name = name
10         self.password = password
11         self.status = status
12 
13     def info(self):
14         if self.status == 0:
15             print('您的账户已锁,请稍后再试')
16         else:
17             print('账户信息:用户名,密码')
18             print(f'{self.name},{self.password}')
19 
20     def modify_password(self):
21         if self.status == 0:
22             print('您的账户已锁,请稍后再试')
23         else:
24             for i in range(3):
25                 old_password = input('输入旧密码:')
26                 if old_password == self.password:
27                     new_password = input('输入新密码:')
28                     self.password = new_password
29                     print('密码修改成功')
30                     break
31                 else:
32                     print('密码输入错误')
33             else:
34                 self.status = 0
35                 print('账户已锁,请稍后再试')
36 
37 
38 class Admin(User):
39     '''
40     管理员账户,继承自User
41     数据:用户名,密码
42     操作:重置密码,封禁账户,解封账户
43     '''
44     def __init__(self, name='admin', password='999999', status=1):
45         super().__init__(name, password, status)
46 
47     def reset_password(self, user_name):
48         if self.name == user_name:
49             self.password = '999999'
50 
51     def ban_user(self, user_name):
52         if self.name == user_name:
53             self.status = 0
54 
55     def unblock_user(self, user_name):
56         if self.name == user_name:
57             self.status = 1
58 
59 
60 def main():
61     x1 = User('Cooper','75641')
62     x1.info()
63     x1.modify_password()
64 
65     x2 = Admin('Jock','674577')
66     x2.info()
67     x2.reset_password('Cooper')
68     x2.info()
69     x2.reset_password('Jock')
70     x2.info()
71     x2.ban_user('Jock')
72     x2.unblock_user('Jock')
73 
74 if __name__ == '__main__':
75     main()
 1 from user import User, Admin
 2 
 3 x1 = User('Joe', '5006692')
 4 x1.info()
 5 x1.modify_password()
 6 x1.info()
 7 
 8 x2 = Admin('Bob', '5002311')
 9 x2.reset_password('Bob')
10 x2.ban_user('Bob')
11 x2.unblock_user('Bob')

 

 

 



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


扫一扫关注最新编程教程