1. 程式人生 > >路飛學城14天集訓營作業1—登陸認證

路飛學城14天集訓營作業1—登陸認證

pass acc clas 分享圖片 name hide 主函數 spl rip

作業一:登陸認證
基礎需求: 讓用戶輸入用戶名密碼 認證成功後顯示歡迎信息 輸錯三次後退出程序 升級需求: 可以支持多個用戶登錄 (提示,通過列表存多個賬戶信息) 用戶3次認證失敗後,退出程序,再次啟動程序嘗試登錄時,還是鎖定狀態(提示:需把用戶鎖定的狀態存到文件裏)

初始版本:
技術分享圖片
 1 # -*- coding:utf-8 -*-
 2 # author: heimu
 3 
 4 import os
 5 
 6 user_dic = {"heimu_1":"111111","heimu_2":"222222","heimu_3":"333333"}
 7 lock_state = []
8 count = 1 9 if os.path.isfile("lock_state.txt"): 10 with open("lock_state.txt","r") as f: 11 lock_state = f.readline() 12 while count <= 3: 13 user_name_input = input("please input user name: ") 14 passwords_input = input("please input six digital passwords: ") 15 if user_name_input in
lock_state: 16 print("Sorry,%s account has been locked" % user_name_input) 17 break 18 elif user_name_input in user_dic and user_dic[user_name_input] == passwords_input: 19 print("Welcome!%s" % user_name_input) 20 break 21 elif count < 3: 22 print("
Authentication failure! Please input again.") 23 else: 24 print("You have failed three times,the program is going to exit !!!") 25 if user_name_input in user_dic: 26 with open("lock_state.txt","a") as f: 27 f.write(user_name_input) 28 count+=1
View Code

經過大王的悉心教誨之後的版本

技術分享圖片
 1 # -*- coding:utf-8 -*-
 2 # author: heimu
 3 
 4 import os
 5 
 6 user_dic = {"heimu_1":"111111","heimu_2":"222222","heimu_3":"333333"}
 7 lock_state = []         # 存儲黑名單
 8 count = 1
 9 first_input_user = None     # 記錄第一次輸入的賬戶名
10 is_same_user = True         # 標誌位,用於判斷三次輸入的用戶名是否相同
11 
12 if os.path.isfile("lock_state.txt"):        # 如果文件存在則打開文件
13     with open("lock_state.txt","r") as f:   # 使用with語句,省去了每次得手動關閉文件
14         for line in f:                      # 叠代f文件,line循環一次表示一行
15             lock_state.append(line.strip()) # .strip 表示取出空格換行符之類的
16 
17 # 主函數
18 while count <= 3:
19     user_name_input = input("please input user name: ")
20     passwords_input = input("please input six digital passwords: ")
21     if user_name_input in lock_state:       # 如果賬戶存在於黑名單,直接退出
22         exit("Sorry,%s account has been locked" % user_name_input)
23     if not first_input_user:                # 如果為None,代表為第一次輸入賬戶
24         first_input_user = user_name_input
25     if first_input_user != user_name_input: # 代表本次輸入的用戶名和第一次用戶名不一樣
26         is_same_user = False                # 標誌位設置為False,表示不再執行賬戶鎖定操作
27     if user_name_input in user_dic and user_dic[user_name_input] == passwords_input:
28         print("Welcome!%s" % user_name_input)   # 登陸成功
29         break
30     else:
31         print("Authentication failure! Please input again.")    # 失敗錯誤少於三次,打印認證失敗語句
32     count+=1
33 else:
34     print("You have failed three times,the program is going to exit !!!")
35     if is_same_user:        # 鎖定用戶
36         if user_name_input in user_dic:     # 如果賬戶名存在於文件中,把賬戶名加入黑名單
37             print("%s賬戶已經鎖定" % user_name_input)
38             with open("lock_state.txt","a") as f:
39                 f.write(user_name_input+\n)
40         else:
41             pass                            # 如果賬戶名不在文件中,就算重復三次也不加入黑名單
View Code

路飛學城14天集訓營作業1—登陸認證