zoukankan      html  css  js  c++  java
  • 用装饰器做一个登陆功能(进阶):

     1 dic = {
     2     'username':None,
     3     'status':False
     4 }
     5 def login(flag):     #  传入 '微信','QQ'
     6     def wrapper(f):
     7         def inner(*args,**kwargs):
     8             if dic['status'] == True:
     9                 ret = f()
    10                 return ret
    11             else:
    12                 i = 0
    13                 while i < 3:
    14                     username = input('请输入你的%s账号:' % (flag)).strip()
    15                     password = input('请输入你的%s密码:' % (flag)).strip()
    16                     with open('login_msg',encoding='utf-8') as f1:
    17                         content = eval(f1.readline())   #eval() 将文件中的内容直接取出赋值给content
    18                         if username == content[flag]['username'] and password == content[flag]['password']:  #falg 的目的就是接受传入的内容是'微信'还是'QQ'
    19                             print('登陆成功')
    20                             dic['username'] = username
    21                             dic['status'] = True   #改变状态
    22                             ret = f()
    23                             return ret
    24                         else:
    25                             print('你输入的账号或密码错误,你还有%d次机会,请重新输入...' % (2-i))
    26                             i += 1
    27         return inner
    28     return wrapper
    29 @login('微信')
    30 def taobao_home():
    31     print('淘宝首页')
    32 @login('微信')
    33 def taobao_shop():
    34     print('淘宝商城')
    35 @login('qq')
    36 def jingdong_home():
    37     print('京东首页')
    38 @login('qq')
    39 def jingdong_shop():
    40     print('京东商城')
    41 
    42 choice_list = {          #定义一个choice_list 用数字作key 函数名作值。
    43     1:taobao_home,
    44     2:taobao_shop,
    45     3:jingdong_home,
    46     4:jingdong_shop
    47 }
    48 
    49 while True:
    50     print('1	淘宝首页
    2	淘宝商城
    3	京东首页
    4	京东商城')   #循环打印四个内容
    51     choice = input('请输入你的选项:').strip()
    52     if choice.isdigit():
    53         choice = int(choice)
    54         if  0 < choice <= len(choice_list):
    55             choice_list[choice]()   #  通过选择的数字 执行相应的函数
    56         else:
    57             print('你输入的不在选项范围内,请重新输入...')
    58     else:
    59         print('你输入的不是数字,请重新输入...')
  • 相关阅读:
    安装@vuecli "失败"
    随缘更新codeforces题解
    四边形不等式
    斯特林数与幂
    待补队列
    IOC容器Autofac的另类使用
    Qt4.x 手工编译及集成到VS2010
    发段代码,验证码,很久以前的,拿出来共享啦。
    WCF 第六章 序列化和编码 总结
    WCF 第六章 序列化与编码之XmlSerializer
  • 原文地址:https://www.cnblogs.com/stfei/p/8710785.html
Copyright © 2011-2022 走看看