跟文件結合的驗證登陸

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 Falsedef 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 Falseprint("歡迎來到德萊聯盟")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("註冊成功")
相關文章
相關標籤/搜索