python学习11——周末练习

# 二:周末综合作业:
# 2.1:编写用户登录接口
# 1、输入账号密码完成验证,验证通过后输出"登录成功"
# 2、可以登录不同的用户
# 3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
count = 0
tag = True
lock_list=[]
with open('info.txt',mode='r',encoding='utf-8') as info_f,\
open('ban.txt',mode='r',encoding='utf-8')as lock_f:
while tag:
inp_name = input('输入用户名')
inp_psd = input('输入密码')
for lock in lock_f:
lock_list.append(lock)
if inp_name in lock_list:
print('你已被锁定')
tag= False
for line in info_f:
name,psd = line.strip().split(':')
if inp_name== name and inp_psd==psd :
print('登陆成功')
tag = False
break
elif inp_psd !=psd:
print('登录失败')
count+=1
if count ==3:
with open('ban.txt',mode='a',encoding='utf-8') as ban_f:
ban_f.write(inp_name)



# 2.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
tag = True
lock_list = []
with open('info.txt', mode='r', encoding='utf-8') as info_f, \
open('ban.txt', mode='r', encoding='utf-8')as lock_f:
while tag:
inp_name = input('输入用户名')
inp_psd = input('输入密码')
for lock in lock_f:
lock_list.append(lock)
if inp_name in lock_list:
print('你已被锁定')
tag = False
for line in info_f:
name, psd = line.strip().split(':')
if inp_name == name and inp_psd == psd:
print('登陆成功')
tag = False
break
elif inp_psd != psd:
print('登录失败')
count += 1
if count == 3:
with open('ban.txt', mode='a', encoding='utf-8') as ban_f:
ban_f.write(inp_name)
elif cmd == '2':
# 注册功能代码
reg_name = input('创建用户名').strip()
reg_psd = input('输入密码').strip()
with open('reg.txt', mode='a', encoding='utf-8') as rg_f, \
open('info.txt', mode='r', encoding='utf-8')as user_f:
for line in user_f:
username, userpsd = line.strip().split(':')
if reg_name in username:
print('用户名已存在')
else:
rg_f.write('{}:{}\n'.format(reg_name, reg_psd))

else:
print('输入的命令不存在')

# 思考:上述这个if分支的功能否使用其他更为优美地方式实现

猜你喜欢

转载自www.cnblogs.com/heirenxilou/p/12497174.html
今日推荐