Day1:Python編寫用戶登陸界面


在做Alex老師第十四期培訓班的課后作業,涉入不深,代碼比較笨拙,請各位多多指教!

filename = "UsernameAndPassword.txt" #存放用戶名及密碼的偽數據庫
user_dict = {}
try: #從數據庫中讀取用戶名密碼,存放在緩存字典中
with open(filename,"r") as dict_file:
for line in dict_file:
(username,password) = line.split(":") #用戶名和密碼的存放格式: alex:abc123
user_dict[username] = password
except IOError as ioerror:
print("{file}not exit".format(file = filename))
namelist = []
try: #從數據庫中讀取已經被鎖定的用戶名單,存放在列表中
with open("list.txt","r") as list_file:
for eachname in list_file:
namelist.append(eachname.strip("\n"))
except IOError as ioerror:
print("list.txt not exit")
while True:
count = 0
name = input("Please input username:") #輸入用戶名
if name in namelist: #判斷用戶名是否被鎖定
print("{0} has been locked!".format(name)) #如果用戶名被鎖定,跳的鎖定界面
user_confirm = input("Do you want try again other username?(Y/N):")#詢問用戶是否想繼續進行嘗試
if user_confirm.lower() == "y":
continue
else:
break
if name in user_dict.keys(): #判斷用戶忙是否存在數據庫中
while count < 3:
_password = input("Please input password:")
if _password == user_dict[name]: #用戶名密碼正確跳轉到登陸成功界面
print("Login success!")
break
else:
count += 1
namelist.append(name) #三次密碼錯誤后,鎖定用戶名
with open("list.txt","a") as add_list:
add_list.write("\n" + name) #並將鎖定的用戶名追加到偽數據庫中
else:
print("username not exit!")

存有用戶名密碼的文本文件和被鎖定用戶的文件與程序在同一文件夾下。

在網數據庫中追加鎖定用戶名時用的 f.open(filename,"a")     "a"表示追加,可以保證原數據庫中的記錄不被替換

 
        

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM