7.5.1
''' 一个类User 数据: 用户名、密码、帐号状态 操作:显示账户信息、修改密码 ''' class User: '''判断密码正误''' def __init__(self,name='guest',password='111111',status='1'): self._name=name self._password=password self._status=status def modify_password(self): a=0 while True: m=input('请输入旧的密码:') if a<2: if m==self._password: n=input('请输入新密码:') self._password=n break elif m!=self._password: print('密码错误,请再次输入') a+=1 elif a==2: print('账户已锁,请稍后再试') self._status=0 break def info(self): print(f'用户名:{self._name}') print(f'密码:{self._password}') if self._status==0: print(f'状态:{self._status},账户异常') elif self._status==1: print(f'状态:{self._status},账户正常') class Admin(User): '''管理员''' def __init__(self,name='Admin',password='999999',status='1'): super().__init__(name, password, status) def info(self): print('管理员账户:') super().info() def reset_password(self,user): user._password=input('请输入你要修改的密码:') def ban_user(self,user): user._status='0' print(f'状态:{user._status},账户异常') def unlock_user(self,user): user._status='1' print(f'状态:{user._status},账户正常')
7.5.2
from user import User,Admin a1=User('ztf','123456',0) a1.modify_password() a1.info() print( ) admin1=Admin('zzz', '2345', '1') admin1.info() admin1.reset_password(a1) admin1.unlock_user(a1) a1.info() admin1.ban_user(a1) a1.info()