实验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     def info(self):
 7         print('账户信息:用户名,密码,账号状态')
 8         if self.status==1:
 9             print(f'{self.name},{self.password},账号状态正常')
10         elif self.status==0:
11             print(f'{self.name},{self.password},账号状态封禁')
12     def modify_password(self,count=1):
13         while count<=3:
14             old_password=eval(input('请先输入旧密码:'))
15             if old_password==self.password:
16                 new_password=eval(input('请输入新密码:'))
17                 self.password=new_password
18                 print('密码修改成功')
19                 break
20             else:
21                 count+=1
22                 if count==4:
23                     print('账户已锁,请稍后再试')
24                 continue
25 class Admin(User):
26     def __init__(self,name='admin',password=999999,stasus=1):
27         super().__init__(name,password,stasus)
28     def info(self):
29         if self.status == 1:
30             print(f'{self.name},{self.password},正常账号')
31         elif self.status == 0:
32             print(f'{self.name},{self.password},封禁账号')
33     def reset_password(self,User):
34         self.password=111111
35     def ban_user(self,User):
36         self.status=0
37     def unblock_user(self,User):
38         self.status=1
39 
40 def main():
41     a1=User('John',123456)
42     a1.info()
43     a1.modify_password()
44     a1.info()
45     a2=Admin('Bob',123456)
46     a2.info()
47     a2.reset_password(a1)
48     a2.ban_user(a1)
49     a2.unblock_user(a1)
50     a2.info()
51 if __name__=='__main__':
52     main()

task5.py

 1 from user import User
 2 a1=User('Mimi',123456)
 3 a1.info()
 4 a1.modify_password()
 5 from user import Admin
 6 a2=Admin('Seho',147896)
 7 a2.info()
 8 a2.reset_password(User)
 9 a2.info()
10 a2.ban_user(User)
11 a2.info()
12 a2.unblock_user(User)
13 a2.info()

实验总结:好晕,python好难啊!

posted @ 2022-06-06 23:07  虞渊  阅读(13)  评论(2编辑  收藏  举报