装饰器的应用

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("输入有误. 请重新选择!")

  

posted @ 2018-12-14 16:38  =-=-  阅读(147)  评论(0编辑  收藏  举报