环境小硕的转化之路-28-面向对象编程方法的适应性训练
前言
这是面向对象编程的一些适应性训练,做完了就会产生新的思维->面向对象编程的思维。
作业一
'''
补充代码实现:
user_list = []
while True:
user = input(“请输入用户名:”)
pwd = input(“请输入密码:”)
email = input(“请输入邮箱:”)
1. while循环提示用户输入:用户名、密码、邮箱(正则满足邮箱格式)
2. 为每个用户创建一个对象,并添加到列表中。
3. 当列表中的添加了3个对象后,跳出循环并以此循环打印所有用户的姓名和邮箱。如:
'''
import re
user_list = []
class User_info:
def __init__(self,name,password,email):
self.name = name
self.password =password
self.email = email
pass
while True:
user = input('请输入用户名:')
pwd = input('请输入密码:')
email = input('请输入邮箱:')
check_ret = re.search('[0-9a-zA-Z][\w\-._]*@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)*\.[a-zA-Z]{2,6}',email)
if check_ret:
user_info = User_info(user,pwd,email)
user_list.append(user_info)
else:
print('please input correct email address.')
if len(user_list) == 3:
for i in user_list:
print('我的名字是%s,邮箱为%s'%(i.name,i.email))
break
作业二
'''
补充代码:实现用户的注册和登陆
'''
class User:
def __init__(self,name,pwd):
self.name = name
self.pwd = pwd
pass
class Account:
def __init__(self):
self.user_list = []#用户列表,数据格式:[Usr对象,usr对象]
self.black_list = []#黑名单用户列表
def login(self):
'''
用户登陆,用户输入用户名和密码并去user_list中检查用户是否合法
:return:
'''
usrname = input('Please input the username.')
usrpassword = input('Please input the userpassword.')
c = 0
while c <=3:
for i in self.user_list:
if usrname in self.black_list:
print('oops,the account is locked.')
exit()
elif usrname == i.name and usrpassword == i.pwd:
print('login successfully')
exit()
elif usrname == i.name and usrpassword != i.pwd:
print('wrong password')
c+=1
else:
print('The account do not exist. ')
self.black_list.append(usrname)
def register(self):
'''
用户注册,动态创建user对象,并添加到self.user_list中
:return:
'''
usrname = input('Please input the user that you want to register.')
usrpassword = input('Please input the userpassword that you want to register.')
temporary_list = []
for i in self.user_list:
temporary_list.append(i.name)
if usrname not in temporary_list:
user_info = User(usrname, usrpassword)
self.user_list.append(user_info)
else:
print('The username had been used.')
def run(self):
'''
主程序,先进行两次用户注册,再进行用户登陆(3次重试机会)
:return:
'''
for i in range(2):
self.register()
self.login()
if __name__ == '__main__':
obj = Account()
obj.run()

浙公网安备 33010602011771号