1、编写用户登录接口
1)输入账号密码完成验证,验证通过后输出"登录成功"
2)可以登录不同的用户
3)同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
user_info = {
'egon':{'password':'123','counter':0},
'tank':{'password':'321','counter':0},
'alex':{'password':'888','counter':0},
'vincent':{'password':'777','counter':0}
}
flag = True
while flag:
inp_name = input('请输入你的账号:').strip()
inp_pwd = input('请输入你的密码:').strip()
file_name = 'locked_users.txt'
with open(r'{}'.format(file_name),mode='rt',encoding='utf-8') as f:
locked_names = f.readlines()
for locked_name in locked_names:
locked_name = locked_name.strip()
if inp_name == locked_name:
print('账号{}已锁定。'.format(inp_name))
flag = False
break
else:
if inp_name in user_info:
if inp_pwd == user_info.get(inp_name).get('password'):
print('登录成功')
break
else:
print('密码错误。')
user_info.get(inp_name)['counter'] += 1
if user_info.get(inp_name).get('counter') == 3:
print('账号{}3次密码输入错误,账号已锁定。'.format(inp_name))
file_name = 'locked_users.txt'
with open(r'{}'.format(file_name),mode='at',encoding='utf-8') as f:
f.write('{}
'.format(inp_name))
break
else:
print('账号不存在,请重新输入。')
2、编写程序实现用户注册后,可以登录。
while True:
msg = """
0 退出
1 登录
2 注册
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue
if cmd == '0':
break
elif cmd == '1':
# 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
count = 0
flag = True
while flag:
inp_name = input('请输入你的账号:').strip()
inp_pwd = input('请输入你的密码:').strip()
with open(r'locked_name.txt',mode='rt',encoding='utf-8') as f:
locked_names = f.readlines()
for locked_name in locked_names:
locked_name = locked_name.strip()
if inp_name == locked_name:
print('账号 {} 已锁定。'.format(inp_name))
flag = False
break
else:
with open(r'db.txt',mode='rt',encoding='utf-8') as f:
users = f.readlines()
user_info = {}
for user in users:
user_name,password = user.strip().split(':')
user_info[user_name] = password
if inp_name in user_info:
if inp_pwd == user_info.get(inp_name):
print('登录成功。')
break
else:
print('密码错误。')
count += 1
if count ==3:
print('账号 {} 3次输入密码错误,账号已锁定。'.format(inp_name))
with open(r'locked_name.txt',mode='at',encoding='utf-8') as f:
f.write('{}
'.format(inp_name))
break
else:
print('你输入的账号不存在,请重新输入。')
elif cmd == '2':
# 注册功能代码
inp_name = input('请输入你要注册的账号:').strip()
inp_pwd = input('请为你的账号设置密码:').strip()
file_name = 'db.txt'
with open(r'{}'.format(file_name),mode='rt',encoding='utf-8') as f:
users = f.readlines()
user_names = []
for user in users:
user_name,*_ = user.strip().split(':')
user_names.append(user_name)
if inp_name in user_names:
print('账号已存在,注册失败。')
else:
with open(r'{}'.format(file_name),'at',encoding='utf-8') as f:
f.write('{}:{}
'.format(inp_name,inp_pwd))
print('注册成功。')
else:
print('输入的命令不存在')
思考题:怎么让第2题的if-elif-else变得更加优美?
思路:让if-elif-else变得优美,其实就是减少条件的数量,即减少elif的数量;elif只能够运行一个,由此想到使用成员运算in,因此考虑将elif的内容存入列表或字典中,使用字典的话则更加灵活方便;字典的value肯定不能是一堆需要运行的代码,考虑将elif的功能封装成函数,函数名即为 value。
# 定义一个注册功能函数register
def register():
inp_name = input('请输入你要注册的账号:').strip()
inp_pwd = input('请为你的账号设置密码:').strip()
file_name = 'db.txt'
with open(r'{}'.format(file_name),mode='rt',encoding='utf-8') as f:
users = f.readlines()
user_names = []
for user in users:
user_name,*_ = user.strip().split(':')
user_names.append(user_name)
if inp_name in user_names:
print('账号已存在,注册失败。')
else:
with open(r'{}'.format(file_name),'at',encoding='utf-8') as f:
f.write('{}:{}
'.format(inp_name,inp_pwd))
print('注册成功。')
# 定义一个登录功能函数log_in
def log_in():
count = 0
flag = True
while flag:
inp_name = input('请输入你的账号:').strip()
inp_pwd = input('请输入你的密码:').strip()
with open(r'locked_name.txt',mode='rt',encoding='utf-8') as f:
locked_names = f.readlines()
for locked_name in locked_names:
locked_name = locked_name.strip()
if inp_name == locked_name:
print('账号 {} 已锁定。'.format(inp_name))
flag = False
break
else:
with open(r'db.txt',mode='rt',encoding='utf-8') as f:
users = f.readlines()
user_info = {}
for user in users:
user_name,password = user.strip().split(':')
user_info[user_name] = password
if inp_name in user_info:
if inp_pwd == user_info.get(inp_name):
print('登录成功。')
break
else:
print('密码错误。')
count += 1
if count ==3:
print('账号 {} 3次输入密码错误,账号已锁定。'.format(inp_name))
with open(r'locked_name.txt',mode='at',encoding='utf-8') as f:
f.write('{}
'.format(inp_name))
break
else:
print('你输入的账号不存在,请重新输入。')
#主程序部分
while True:
msg = """
0 退出
1 登录
2 注册
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
cmd_dic = {
'1':log_in,
'2':register
}
if cmd == '0':
break
elif cmd in cmd_dic:
cmd_dic.get(cmd)()
else:
print('只能输入命令编号的数字,傻叉')
总结:虽然在这个列子中看不出来这种做法有什么很大的作用,但是实际上是因为这个例子中的elif只有两个,假设有很多个elif,相信这种方法的美化作用将十分明显。