动态页面的登陆——装饰器应用<代码>

log_status = False
f = open("info", "r", encoding="utf8")
source = f.read()
source = eval(source)
username = ""
def info_match(f_para_info):
    username = input("type your ID:")
    password = int(input("type your password:"))
    global source
    if username == source[f_para_info]["name"] and password == source[f_para_info]["password"]:
        print("login successful")
        global log_status
        log_status = True
    else:
        print("login failed")
def para_decorator(f_para_pd):
    def decorator(f_para_dec):
        def func():
            if log_status == True:
                global source
                sf = source[f_para_pd]["name"]
                print("welcome user {kk}".format(kk=sf))
                f_para_dec()
            if log_status == False:
                if f_para_pd == "jingdong":
                    info_match(f_para_pd)
                    f_para_dec()
                elif f_para_pd == "weixin":
                    info_match(f_para_pd)
                    f_para_dec()
                elif f_para_pd == "qq":
                    info_match(f_para_pd)
                    f_para_dec()
        return func
    return decorator
@para_decorator("jingdong")  # homepage=para_decorator("jingdong")(homepage)
def homepage():
    print("welcome to home...")
@para_decorator("weixin")
def finance_page():
    print("welcome to finance")
@para_decorator("qq")
def bookstore():
    print("enjoy to the sea of knowledge")
print("welcome to jd.com".center(60,"*"))
while True:
    print('''
    1.homepage (账号:ccc 密码:123)
    2.finance_page(账号:gaga 密码:456)
    3.bookstore(账号:xiaomu 密码:789)
    4.quit to jd
    '''
    )
    switch = int(input("switch to ..."))
    if switch == 1:
        homepage()
    elif switch == 2:
        finance_page()
    elif switch == 3:
        bookstore()
    elif switch == 4:
        break

功能:不同的页面需要不同的账号来登陆;登陆后返回等操作后不需要二次登陆。

posted @ 2016-09-14 15:15  gege4105  Views(240)  Comments(0Edit  收藏  举报