標籤:python form orm 分享 後退 word pen color ase
基礎需求:讓使用者輸入使用者名稱密碼認證成功後顯示歡迎資訊輸錯三次後退出程式升級需求:可以支援多個使用者登入 (提示,通過列表存多個賬戶資訊)使用者3次認證失敗後,退出程式,再次啟動程式嘗試登入時,還是鎖定狀態(提示:需把使用者鎖定的狀態存到檔案裡)
代碼:
# 通過列表格儲存體使用者名稱,密碼logon_authentication = Falseuser_info = [[‘li‘, ‘123‘], [‘yong‘, ‘234‘], [‘liyong‘, ‘345‘]]logon_num = 3# 取鎖定名單lock_file = open(‘lock‘, ‘r‘, encoding=‘utf-8‘)lock_list = lock_file.read()lock_file.close()# 首次輸入使用者密碼user_name = input(‘please input your name :‘)user_pass = input(‘please input your password :‘)# 迴圈判斷使用者密碼for user_item in user_info: if user_name == user_item[0]: # 判斷是否在鎖定名單 if user_name in lock_list: print(‘該使用者已被鎖定!‘) break # 驗證使用者密碼,重新輸入密碼須在限制次數內 for i in range(logon_num - 1): if user_pass == user_item[1]: print(‘登陸成功‘) # 標誌位驗證登入結果及跳出多層迴圈 logon_authentication = True break else: logon_num = logon_num - 1 print(‘密碼錯誤,您還有‘, logon_num, ‘次輸入密碼的機會‘) user_pass = input(‘please again input your password :‘) else: print(‘超過三次,使用者鎖定!‘) lock_file = open(‘lock‘, ‘a‘, encoding=‘utf-8‘) lock_list = lock_file.write(user_name) lock_file.close() break # 判斷跳出一級迴圈 if logon_authentication: breakelse: print(‘沒有此使用者‘)# 登入成功,進入下一級頁面if logon_authentication: print(‘歡迎來到{user}的空間‘.format(user=user_name))View Code
Python練習(第一周): 編寫登陸認證程式