Python 裝飾器的應用
阿新 • • 發佈:2018-12-15
驗證 input 添加 light 校驗 gin 退出 pytho glob
menu = ("查看", "添加", "修改", "刪除", "退出") flag = False # 沒登錄 def login(): global flag username = input("請輸入用戶名:") password = input("請輸入密碼:") if username == "alex" and password == "123": flag = True print("登錄") else: flag = False print("用戶名密碼錯誤") # 登錄驗證裝飾器 def login_verify(fn): def inner(*args, **kwargs): # 登錄校驗 while 1: if flag == True: ret = fn(*args, **kwargs) return ret else: print(‘對不起, 您還沒有登錄‘) login() return inner def chakan(): print("==============================查看") @login_verify def tianjia(): print("============================添加") @login_verify def xiugai(): print("=======================修改") @login_verify def shanchu(): print("=========================刪除") while 1: for i in range(len(menu)): print(i+1, menu[i]) num = input("請輸入你要執行的菜單:") if num == "1": chakan() elif num == "2": tianjia() elif num == "3": xiugai() elif num == "4": shanchu() elif num == "5": print("程序退出中..........") exit() else: print("輸入有誤. 請重新選擇!")
Python 裝飾器的應用