函数__面向对象(相关联系)
==========================
user_list = [] class UserInfo: def __init__(self,username,password,email): self.username = username self.password = password self.email = email while True: user = input("输入用户名:) pwd = input("输入密码:") email = input("输入邮箱:") u = UserInfo(user,pwd,email) #创建对象 user_list.append(u) #把创建好的对象添加到列表 if len(user_list) == 3: break for el in user_ist: #el 是userinfo的对象 print(f"用户名:{el.username},邮箱是:{el.email}")
第二题:=============================================
===========================================
class Account: def __init__(self): self.user_list = [] #目的是存储用户信息 def login(self): for i in range(3): uname = input(">>>") upwd = input(">>>") for u in self.user_list: if unam == u.username and upwd == u.password: print("登陆成功") return else: print("登陆失败") def regist(self): uname = input(">>>") upwd = input(">>>") u = User(uname,upwd) #创建用户 self.user_list.append(u) #可以继续完善,屏蔽掉重复的用户 def run(self): #注册 self.regist() #自己类中的方法互相调用,用self调用 self.regist() self.login() #if __name__=="__main__": #当前py文件如果是启动文件,执行这里的代码 obj = Account() obj.run()