装饰器应用之登录

# filename = "E:\\pythonStudy\\jingdong.txt"
# with open(filename,'a') as f:
    # f.writelines("thirteen:1234\n")
    # print("写入成功!")

# filename = "E:\\pythonStudy\\weixin.txt"
# with open(filename,'a') as f:
    # f.writelines("thirteen2:1234\n")
    # print("写入成功!")

login_stats = False
choice=""

content='''*******************************请 选 择 :***********************************
        1:home 
        2:finance
        3:book
******************************************************************************'''

def chose_auth_type(auth_type="jingdong"):
    def login(function_name):
        def inner():
            global login_stats
            if login_stats is False:
                if input_information(auth_type):
                    print("登录成功!ovo")
                    login_stats = True
                    function_name()
                else:
                    print("登录失败!")
                    print()
            else:
                function_name()
        return inner
    return login

@chose_auth_type("jingdong")
def home():
    print("Welcome to home page!")

@chose_auth_type("weixin")
def finance():#金融
    print("Welcome to finance page!")

@chose_auth_type()
def book():
    print("Welcome to book page!")

def input_information(auth_type):
    username=input("username:").strip()
    passwords=input("passwords:").strip()
    if auth_type == "jingdong":
        print("******************京东账号登录中......*************************************")
        return auth_login(username,passwords,auth_type)
    elif auth_type == "weixin":
        print("******************微信账号登录中......************************************")
        return auth_login(username,passwords,auth_type)

    
def auth_login(username,passwords,auth_type):
#get_information()返回来的值赋给user和password
    rest=get_information(auth_type)
    if username != "q" or passwords != "q":
        if username in rest.keys():
            if rest[username] == passwords:
                return True
            else:
                print()
                print('''*********************用户名或密码错误!请重试!*****************************''')
                input_information(auth_type)
        else:
            print()
            print('''*********************用户名或密码错误!请重试!*****************************''')
            input_information(auth_type)
    else:
        return False

def get_information(auth_type):#根据auth_type参数,从不同的文件中读入用户名和密码
    rest={}
    filename = "E:\\pythonStudy\\"+auth_type+".txt"
    with open(filename) as f:
        for line in f:
            (key, value) = line.strip().split(':')
    #将读取到的一行信息通过“:”分割成为一个列表,之后对应赋值给key,value两个变量
            rest[key] = value
        return rest

while(True):
    print(content)
    choice=input(">>>:")
    if choice == "q":
        print("欢迎再来!=*v*=")
        break
    elif choice == "1":
        home()
    elif choice == "2":
        finance()
    elif choice == "3":
        book()
    else :
        print("输入错误!")
        continue




 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值