上海python周末培训班_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分支的功能否使用其他更为优美地方式实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值