Python實現簡單的使用者登入資訊確認,密碼輸錯3次後,使用者被鎖定

來源:互聯網
上載者:User

標籤:src   been   列表   user   tin   img   分享圖片   trie   wro   

‘‘‘問題描述:
從文檔中讀入使用者名稱和密碼,檢驗使用者名稱和密碼的正確性,
密碼輸錯三次後使用者被鎖定,不允許登陸
解決思路:
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次後,使用者被鎖定

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.