# _*_ coding:utf-8 _*_#coding mr.ding"""需求:1),啓動程序,首頁面應該顯示成以下格式: 歡迎來到博客園首頁 1:請登陸 2:請註冊 3:文章頁面 4:日記頁面 5:評論頁面 6:收藏頁面 7:註銷 8:退出程序2),用戶輸入選項,3~6選項必須在用戶登陸成功以後,才能訪問成功。3),用戶選擇登陸,用戶名密碼從register文件中讀取驗證,三次機會,沒成功則結束整個程 序運行,成功以後,能夠選擇訪問3~6項,訪問頁面以前,必需要在log文件中打印日誌, 日誌格式爲-->用戶:xx 在xx年xx月xx日 執行了 %s函數,訪問頁面時,頁面內容爲:歡 迎xx用戶訪問評論(文章,日記,收藏)頁面4),若是用戶沒有註冊,則能夠選擇註冊,註冊成功以後,能夠自動完成登陸,而後進入首頁選擇。5),註銷用戶是指註銷用戶的登陸狀態,使其在訪問任何頁面時,必須從新登陸。6),退出程序爲結束整個程序運行。"""import timestatus_dict={ 'username':None, 'status':False,}def wrapper_log(l): #日誌裝飾器,用於打印用戶訪問3~6功能日誌。 def inner_log(*args,**kwargs): re = l(*args,**kwargs) while True: with open("log", encoding="utf-8", mode="a") as f2: f2.write("\n用戶: %s 在 %s 執行了 %s 函數,訪問頁面時,頁面內容爲:歡迎%s用戶訪問 %s" % \ (status_dict["username"], time.strftime("%Y-%m-%d %H:%M:%S"),l.__name__,status_dict["username"],choice_dict[choice][0])) return True return re return inner_logdef wrapper_login(loging): #登錄認證裝飾器,用於若是用戶沒有登錄選擇3~6功能會提醒用戶進行登錄。 def inner_loging(*args,**kwargs): reg = loging(*args, **kwargs) if status_dict["username"] is not None and status_dict["status"] == True: print("歡迎"+status_dict["username"]+"用戶,訪問"+choice_dict[choice][0]+"") else: print("您尚未登錄,不能使用此模塊。") return False return reg return inner_logingdef login(): #登錄函數 i = 0 while i < 3: username = input('請輸入用戶名:').strip() password = input('請輸入密碼:').strip() if status_dict["username"] is not None and status_dict["status"] == True: print("用戶"+status_dict["username"]+"已登錄,請註銷後從新登錄。") break with open('register',encoding='utf-8') as f1: for line in f1: line_list = line.strip().replace(',', ',').split(',') if username == line_list[0] and password==line_list[1]: print('登陸成功...') status_dict['username'] = username status_dict['status'] = True return True else: print('帳號或者密碼不正確,請從新輸入') i += 1 if i == 3: exit()def regiter(*args,**kwargs): #註冊函數 while True: username = input('請輸入註冊用戶名:').strip() password = input('請輸入用戶名密碼:').strip() with open('register',encoding='utf-8') as f1: for line in f1: line_list = line.strip().replace(',', ',').split(',') if username == line_list[0]: print('用戶名存在,請從新輸入!') break else: with open('register',encoding='utf-8',mode='a') as f2: f2.write('\n{},{}'.format(username,password)) print('註冊成功') return True#調用裝飾器@wrapper_login@wrapper_logdef article_page(): #文章頁面函數 print("請編輯文章。")@wrapper_login@wrapper_logdef diary_page(): #日誌頁面函數 print("請編輯日誌。")@wrapper_login@wrapper_logdef comment_page(): #評論頁面函數 print("請評論。")@wrapper_login@wrapper_logdef collection_page(): #收藏頁面函數 print("請收藏")def logout(): #註銷函數 print("確認註銷%s用戶?輸入y註銷用戶,輸入n返回。" % status_dict["username"]) while True: user_choice = input("請輸入選項:") if user_choice == "y": status_dict["username"] = None status_dict["status"] = False print("註銷成功,已返回登錄界面。") login() return True elif user_choice == "n": return False else: print("您輸入的選項不正確,請從新輸入。")def exit_program(): #退出程序函數 print("確認要退出程序?輸入y退出,輸入n返回。" % status_dict["username"]) while True: user = input("請輸入選項:") if user == "y": print("退出程序成功。") exit() elif user == "n": return False else: print("您輸入的選項不正確,請從新輸入。")choice_dict = { 1: ['登陸', login], 2: ['註冊', regiter], 3: ['文章頁面',article_page], 4: ['日記頁面', diary_page], 5: ['評論頁面', comment_page], 6: ['收藏頁面', collection_page], 7: ['註銷', logout], 8: ['退出程序',exit_program],}while True: print("歡迎來到博客園") for (k, v) in choice_dict.items(): print(k, v[0]) choice = input("請輸入選項:") if choice.isdigit(): choice = int(choice) if 0 < choice< len(choice_dict)+1: choice_dict[choice][-1]() continue else: print("超出範圍,請從新輸入!")