跟文件结合的验证登录

def login(username,password):
f = open("db","r",encoding="utf-8")
for line in f:
line = line.strip()#默认strip无参数:移除空格换行符;有参数:移除两侧指定的值
line_list = line.split("$$")
if user == line_list[0] and pwd == line_list[1]:
return True
return False
def register(username,password):
#注册用户
#1打开文件2用户名密码
with open("db","a",encoding="utf-8") as f:
temp = "\n" + username + "$$" + password
f.write(temp)
def user_exist(username):
#一行一行查找,如果用户名存在,returnTrue;False
with open("db","r",encoding="utf-8") as f:
for line in f:
line = line.strip()
line_list = line.split("$$")
if line_list[0] == username:
return True
return False
print("欢迎来到德莱联盟")
inp = input("1:登录;2:注册")
if inp == "1":
user = input("请输入用户名:")
pwd = input("请输入密码:")
is_login = login(user,pwd)
if is_login:
print("登录成功")
else:
print("登录失败")
elif inp == "2":
user = input("请输入用户:")
pwd = input("请输入密码:")
is_exist = user_exist(user)
if is_exist:
print("用户已存在,无法注册")
else:
register(user,pwd)
print("注册成功")
posted @ 2017-10-29 12:37  彩色的大卷发  阅读(151)  评论(0编辑  收藏  举报