问题写在代码第一行(第一段代码后有更新),懂的程序员能看一下吗(有点费眼)?
#修复bug:加入黑名单的用户名仍能执行登录程序【代码登录流程部分(66-101行)】
def inital_page():
print("*"*30)
print("编码\t操作")
print("1\t注册")
print("2\t登录")
print("3\t退出")
print("*"*30)
def register_page():
print("注册".center(30,'-'))
print("提示:用户名只能包含数字、字母以及'_'")
#用户名注册
while True:
out = 0
username = input("请输入用户名:")
#判断用户名是否存在
f = open("userlist.txt","a+",encoding="utf-8") #如有非数字以及文字字符,一定要加encoding = "……",否则将默认以编码解码,导致报错
f.seek(0)
data = f.readlines()
for user in data:
if username == user.split(":")[0]:
print("用户名已存在,请重新输入")
out += 1
else:
continue
#判断用户名是否符合规范
for i in list(username):
if i.isalnum() or i == "_":
continue
else:
print("【用户名只能为字母、下划线和数字!】")
out = out + 1
break
#退出该流程
if out == 0:
break
#密码注册
while True:
out = 0
password = input("请输入密码:")
if not(password.isalnum() and len(password) <= 8):
print("【密码只能为数字、字母以及8个字符】")
out = out + 1
else:
password2 = input("请再次输入密码:")
if password2 != password:
print("两次密码不一致")
out += 1
continue
else:
print("注册成功!")
break
if out == 0:
break
#储存用户名和密码到文件中
f = open("userlist.txt","a+",encoding='utf-8')
f.write(f"{username}:{password}"+'\n')
f.close()
#登录流程
def login_page():
name = []
key = []
print("登录".center(30,'-'))
g = open("blacklist.txt","a+",encoding='utf-8')
f = open("userlist.txt","r",encoding='utf-8')
f.seek(0)
word = f.readlines()
for line in word:
name.append(line.split(":")[0].replace("\n",""))
key.append(line.split(":")[1].replace("\n",""))
username1 =input("请输入您的用户名:")
if username1 not in name:
print("用户名不存在,请重新输入!")
else:
times = 3
while True:
password1 = input("请输入您的密码:")
if password1 == key[name.index(username1)]:
print("登陆成功")
break
else:
if times == 2:
times -= 1
print(f"密码错误,请重新输入密码,您还有{times}次机会")
continue
elif times == 1:
print("您的账号已被锁定,请稍后重试")
g.write(username1 + '\n')
break
else:
times -= 1
print(f"密码错误,请重新输入密码")
def program_test():
while True:
inital_page()
oper_code = input("请输入您所需操作的对应编号(1、2、3):")
if oper_code == "1":
register_page()
elif oper_code == "2":
login_page()
elif oper_code == "3":
exit()
else:
print("输入错误,请重新输入!")
program_test()
后在79行和80行之间插入了以下代码,仍然没有达到效果*,想问一下怎么修改才能达到效果呢?
效果:登录流程中,用户输入用户名后,如用户为黑名单中的用户,则提示“账户已锁定”
forbid_users = g.readlines()
print(forbid_users)
for user in forbid_users:
if username1 == user.replace("\n",""):
print("您的账号已被锁定,请联系管理员")
break