爲了之後更好更快速的複習,此博客記錄我對做業的總結。對於基礎做業,我認爲最重要的是過程,至於實現是否是完美,代碼是否是完美,雖然重要,可是做業過程當中,用到的知識點是值得總結和整理的。
1. 用戶輸入賬號密碼進行登錄
2. 用戶信息保存在文件內
3. 用戶密碼輸入錯誤三次後鎖定用戶
python
1)輸入時用戶名後,先判斷用戶名是否被鎖,而後檢查用戶名是否存在用戶數據庫;
2)輸入相同不存在用戶名3次,此用戶名被鎖;
3)輸入用戶名存在時纔可輸入密碼,相同用戶名時,輸入密碼錯誤三次,此用戶名被鎖;
4)若每次都沒輸入存在的用戶,則再次要求輸入用戶,直到達到總輸入次數;git
cat user_login.py #主程序
#!#!/usr/bin/env python #_*_coding:utf-8_*_ ''' * Created on 2016/10/10 22:13. * @author: Chinge_Yang. ''' import os import getpass retry_max = 3 retry_count = 0 user_file = "user.txt" lock_file = "user_lock.txt" user_same = 0 user_tmp = "" if not os.path.exists(lock_file): #不存在時,則建立 f = open(lock_file,'w') f.close() while retry_count < retry_max: #輸入用戶名 user_name = input("Please input your name:") #輸入的用戶名和上次輸入的對比 if user_name == user_tmp: #用戶同樣數加1 user_same += 1 else: #用戶同樣數歸0 user_same = 0 #輸入的用戶名存爲臨時變量 user_tmp = user_name #判斷用戶是否被鎖 lock_check = open(lock_file) for line in lock_check: line = line.split() #用戶被鎖,打印提示 if user_name == line[0]: exit("User \033[1;31;40m%s\033[0m is locked!" % user_name) lock_check.close() #查看是否存在於用戶數據庫 user_check = open(user_file) for l in user_check: l = l.split() user = l[0] passwd = l[1] if user_name == user: #輸入密碼 #user_passwd = input("Please input your password:") user_passwd = getpass.getpass("\033[1;33;40mPlease input your password:\033[0m") #判斷密碼是否正確 if user_passwd == passwd: exit("\033[1;32;40mWelcome to you!\033[0m") else: print("User \033[1;31;40m%s\033[0m password is error!" % user_name) #用戶存在於數據庫,跳出循環 break else: print("User \033[1;31;40m%s\033[0m not match in the user file!" % user_name) user_check.close() if user_same >= 2: print("User \033[1;31;40m%s\033[0m name was locked!" % user_name) #將用戶名寫入到鎖定文件中 file = open(lock_file,"a") file.write(user_name+"\n") file.close() retry_count += 1
user.txt文件內容格式:
算法
cat user.txt
apache 123 jim 234 docker 333 fank yhn
1.getpass模板用於輸入密碼時,不顯示明文;docker
2.os.path.exists(文件路徑)用於判斷文件是否存在;shell
3.input的用法,版本3,默認輸入的內容是字符類型,若是是變量名,則爲此變量名的數據類型;數據庫
4.if判斷語句用法;apache
5.while循環語句用法;服務器
6.split()指定分隔符對字符串切片用法;網絡
7.print(「%s」 % name)等格式化輸出;函數
8.顏色輸出用法:手打出來 \033[32;1m內容\033[0m ;
9.數學算法使用;
1. 運行程序輸出第一級菜單
2. 選擇一級菜單某項,輸出二級菜單,同理輸出三級菜單
3. 菜單數據保存在文件中
1.菜單數據使用字典保存在文件中,使用import讀取;
2.獲取一級菜單;
3.獲取二級菜單;
4.獲取三級菜單;
5.使用循環打印菜單;
cat Three_layer_menu.py #主程序
#!/usr/bin/env python #_*_coding:utf-8_*_ ''' * Created on 2016/10/16 21:31. * @author: Chinge_Yang. ''' import sys import menu while True: #定義一層菜單爲字典 one_dict = {} #獲取字典全部的鍵 one_layer = menu.menu.keys() #獲取字典全部的枚舉 one_enu = enumerate(one_layer) #一層菜單字典 for index1,value1 in one_enu: one_dict[index1]=value1 for k1 in one_dict: print ("\033[32m%d\033[0m --> %s" %(k1,one_dict[k1])) print ("輸入【back】:返回;【quit】:退出") once_select = input("請輸入你的選擇:\n").strip() if once_select.isdigit(): once_select = int(once_select) if 0 <= once_select < len(one_dict): print("---->進入一級菜單 \033[32m%s\033[0m" %(one_dict[once_select])) while True: #定義二層菜單爲字典 two_dict = {} # 獲取字典全部的鍵 two_layer = menu.menu[one_dict[once_select]].keys() # 獲取字典全部的枚舉 two_enu = enumerate(two_layer) # 二層菜單字典 for index2, value2 in two_enu: two_dict[index2] = value2 # 打印二層菜單 for k2 in two_dict: print("\033[32m%d\033[0m --> %s" % (k2, two_dict[k2])) print("輸入【back】:返回;【quit】:退出") twice_select = input("請輸入你的選擇:\n").strip() if twice_select.isdigit(): twice_select = int(twice_select) if 0 <= twice_select < len(two_dict): print("---->進入二級菜單 \033[32m%s\033[0m" % (two_dict[twice_select])) # 定義三層菜單列表 three_layer = menu.menu[one_dict[once_select]][two_dict[twice_select]] while True: # 打印三層菜單 for k3 in three_layer: print (k3) print ("輸入【back】:返回;【quit】:退出") three_select = input("請輸入你的選擇:\n").strip() if three_select == "back": break elif three_select == "quit": sys.exit ("----謝謝使用----") continue else: if twice_select == "back": break elif twice_select == "quit": sys.exit ("----謝謝使用----") print ("\033[31m請輸入數字\033[0m") else: print("----\033[31m數字超出範圍,請從新輸入!\033[0m----") else: if once_select == "back": break elif once_select == "quit": sys.exit ("----謝謝使用----") print ("\033[31m請輸入數字\033[0m")
cat menu.py
#!/usr/bin/env python # _*_coding:utf-8_*_ ''' * Created on 2016/10/16 19:37. * @author: Chinge_Yang. ''' menu = { "家用電器": { "電視": [ "合資品牌電視", "國產品牌電視", "互聯網品牌電視" ], "空調": [ "壁掛式空調", "櫃式空調", "中央空調", "空調配件" ], "洗衣機": [ "滾筒洗衣機", "洗烘一體機", "波輪洗衣機", "迷你洗衣機", "洗衣機配件" ] }, "電腦": { "電腦整機": [ "筆記本", "遊戲本" "平板電腦", "平板電腦配件", "臺式機", "一體機服務器", "筆記本配件" ], "電腦配件": [ "顯示器", "CPU", "主板", "硬盤", "內存" ], "外設產品": [ "鼠標", "鍵盤", "鍵鼠套裝", "網絡儀表儀器", "U盤", "移動硬盤" ] } }
1.字典的用法,包括獲取key,value及相關函數用法;
2.列表的用法,遍歷列表的用法;
3.import導入py文件用法;
本文出自 「ygqygq2」 博客,謝絕轉載!