1. 程式人生 > >跟文件結合的驗證登錄

跟文件結合的驗證登錄

聯盟 strip() als username 文件 encoding utf-8 use def

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("註冊成功")

跟文件結合的驗證登錄