注册
account_lst = []
flag = True
with open("user.txt",mode='r+',encoding='utf-8') as fp:
while flag:
username = input("请输入用户名:")
if username == "" or " " in username:
print("您输入的用户名非法")
else:
fp.seek(0)
lst = fp.readlines()
for i in lst:
account = i.strip().split(":")[0]
account_lst.append(account)
if username in account_lst:
print("用户名已经存在")
else:
pwd = input("请输入密码")
while True:
pwd2 = input("请再次输入密码")
if pwd == pwd2:
strvar = username + ":" + pwd + "\n"
fp.write(strvar)
flag = False
print("注册成功")
break
elif pwd2 == 'q'
break
else:
print("两次密码输入不一致")
登录
# ### 登录
# 1.账户密码会预先记录在文件中
# 2.输入账号密码:如果都正确,登录成功
# 3.如果用户登录时进行三次校验,都不对,记录黑名单文件中
# 4.如果是黑名单的用户,禁止下次再次登录
# 存账户
accountlst = []
# 存密码
pwdlst = []
# 存黑名单
blacklst = []
sign = True
while sign:
username = input("请输入您的用户名:")
# 获取文件当中的账号和密码
with open("user.txt",mode="a+",encoding="utf-8") as fp:
fp.seek(0)
lst = fp.readlines()
for i in lst:
user,pwd = i.strip().split(":")
# print(username,password)
accountlst.append(user)
pwdlst.append(pwd)
# print(accountlst)
# print(pwdlst)
# 判断该用户是否存在
if username in accountlst:
# 获取文件中的黑名单列表
with open("black.txt",mode="a+",encoding="utf-8") as fp:
fp.seek(0)
lst = fp.readlines()
for i in lst:
blacklst.append(i.strip())
# 判断当前用户是否是黑名单中的用户
if username in blacklst:
print("抱歉,该用户已经被冻结了")
else:
# 如何找到该账户对应的密码呢?
num = accountlst.index(username)
pwd = pwdlst[num]
# print(num,pwd)
""""""
# 判断用户输入的密码是否正确
times = 1
while times <= 3:
pwd2 = input("请输入您的密码")
# 输入成功情况下,终止循环,程序结束
if pwd == pwd2:
print("登录成功")
sign = False
break
else:
# 剩余的次数 = 总次数 - 使用次数
print("你还剩下%s次机会" % (3 - times))
# 如果用掉了所有的次数,直接加入黑名单,防止下次登录
if times == 3:
with open("black.txt",mode="a+",encoding="utf-8") as fp:
strvar = username + "\n"
fp.write(strvar)
print("抱歉,您的账户已经被冻结~")
times += 1
else:
print("该用户不存在~")
注册流程图
登录流程图