1. 程式人生 > >Python實現簡單的用戶登錄信息確認,密碼輸錯3次後,用戶被鎖定

Python實現簡單的用戶登錄信息確認,密碼輸錯3次後,用戶被鎖定

and 技術分享 swd gin 計數 lse img success python

‘‘‘問題描述:
從文檔中讀入用戶名和密碼,檢驗用戶名和密碼的正確性,
密碼輸錯三次後用戶被鎖定,不允許登陸
解決思路:
1.讀用戶信息文檔,存入二維列表中,
2.需要把鎖定的拉入黑名單
3.只有用戶名輸對的情況下才可以輸入密碼,密碼輸錯三次用戶就被鎖定
‘‘‘


#User_Info存用戶名和密碼,Lock_Info存被鎖定的用戶
file = open(r"User_Info.txt","r")
file2 = open(r"Lock_Info.txt","r+")
user = file.readlines()#讀用戶名和密碼
black_user =file2.readlines()#存黑名單
_user = []
_black_user = []


#把user_info,txt的用戶信息存入列表_user
for info in user:
temp = info.strip(‘\n‘)
temp2 = temp.split(‘\t‘)
_user.append(temp2)
#把user_info,txt的用戶信息存入列表_user
for info in black_user:
temp = info.strip(‘\n‘)
temp2 = temp.split(‘\t‘)
_black_user.append(temp2)


while True:
username = input("Please input your username:")
#先判斷用戶是否在黑名單中
for i in range(len(_black_user)):
if username == _black_user[i][0]:
print("user has been locked")
exit() # 直接退出
#當不再黑名單時。判斷在不在user_info中,如果不再就不停輸入用戶名
for i in range(len(_user)):
if i == len(_user) - 1 and username != _user[len(_user) - 1][0]:
print("username does not exist;")
break
if username != _user[i][0]:
continue
else:
count = 0 #密碼計數器
passwd = input("Please input your passwd:")
while count < 3:
if passwd == _user[i][1]:
print("Successfully login in...")
exit() #break只是退出while循環,仍會進行下次for循環,所以登陸成功直接exit()退出程序
elif count < 2:
print("wrong passwd.please input again.")
passwd = input("Please input your passwd:")
count +=1
else:
print("you have tried 3 times.your acount have been locked...")
file2.write(_user[i][0] + "\t" + _user[i][1] + "\n")#被鎖後用戶信息讀到中
exit()
file.close()
file2.close()

技術分享圖片

Python實現簡單的用戶登錄信息確認,密碼輸錯3次後,用戶被鎖定