吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2623|回复: 2
收起左侧

[Python 转载] python装饰器的问题

[复制链接]
ddy4633 发表于 2017-10-19 14:14
[Python] 纯文本查看 复制代码
# 装饰器实现登入
loggin_status=False
list=[]
def loggin(func):
    if loggin_status == False:
        def loggin_in():
            user=input('user:')
            passwd=input('password:')
            with open('账号信息','r',) as file:
                for i in file.readlines():
                    list.append(i[i.find('=')+1:].strip('\n'))
                    pass
            if list.count(user) is not 0 and list.count(passwd) is not 0:
               print('login.............')
               global loggin_status
               loggin_status=True
            else:
                print('err')
        return loggin_in

@loggin
def shouji():
    print('this pages is shouji')
@loggin
def dianqi(list):
    print('this pages is dianqi')
while True:
    print('''
    欢迎使用
    1.手机
    2.电气
    3.退出
    ''')
    a=int(input('>>'))
    if a in range(1,4):
        if a==1:
            shouji()
            if loggin_status is False:
                print('loggin_status为False')
            else:
                print('loggin_status为Ture')
        elif a==2:
            dianqi()
        elif a==3:
            break
    else:
        print('不再范围内....')

以上代码本意是想实现,选择后判断是否登入如果登入了就不需要再一次登入,如果没有登入则执行装饰器登入。但是没有实现,请大神指点一下!
以下为执行后的结果:
QQ截图20171019141338.jpg

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

飛越無限 发表于 2017-10-23 17:29
[Python] 纯文本查看 复制代码
# 装饰器实现登入
loggin_status = False
list = []


# def loggins(func):
#     if loggin_status == False:
#         def loggin_in():
#             user = input('user:')
#             passwd = input('password:')
#             with open('账号信息', 'r', ) as file:
#                 for i in file.readlines():
#                     list.append(i[i.find('=') + 1:].strip('\n'))
#                     pass
#             if list.count(user) is not 0 and list.count(passwd) is not 0:
#                 print('login.............')
#                 global loggin_status
#                 loggin_status = True
#             else:
#                 print('err')
#
#         return loggin_in


def login():
    global loggin_status
    user = input('user:')
    passwd = input('password:')
    if user == 'king' and passwd == '123456':
        print("登陆成功!!!")
        loggin_status = True
    else:
        print("密码错误!!!!")
        loggin_status = False

def loggin(func):
    def warpper(*args, **kwargs):
        if not loggin_status:
            login()

        func()

    return warpper

@loggin
def shouji():
    print('this pages is shouji')


@loggin
def dianqi():
    print('this pages is dianqi')


while True:
    print('''
    欢迎使用
    1.手机
    2.电气
    3.退出
    ''')
    a = int(input('>>'))
    if a in range(1, 4):
        if a == 1:
            shouji()
            if loggin_status is False:
                print('loggin_status为False')
            else:
                print('loggin_status为Ture')
        elif a == 2:
            dianqi()
        elif a == 3:
            break
    else:
        print('不再范围内....')

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
ddy4633 + 1 + 1 谢谢了,我觉得集成在一起更好

查看全部评分

飛越無限 发表于 2017-10-23 23:15
[Python] 纯文本查看 复制代码
# 装饰器实现登入
loggin_status = False
user_dict = {}

# 数据库密码 文件格式
# user_db
# king=123456

# def login():
#     global loggin_status
#     user = input('user:')
#     passwd = input('password:')
#     if user == 'king' and passwd == '123456':
#         print("登陆成功!!!")
#         loggin_status = True
#     else:
#         print("密码错误!!!!")
#         loggin_status = False


def login():
    global loggin_status
    user = input('user:')
    password = input('password:')

    with open('user_db', 'r', encoding='utf-8') as file:
        for line in file.readlines():
            u, p = line.strip().split('#')
            if user == u and password == p:
                print("登陆成功!!!")
                loggin_status = True
                break
        else:
            print("用户密码错误!!!")
            loggin_status = False


def loggin(func):
    def warpper(*args, **kwargs):
        if not loggin_status:
            login()
        func()

    return warpper


@loggin
def shouji():
    print('this pages is shouji')


@loggin
def dianqi():
    print('this pages is dianqi')


while True:
    print('''
    欢迎使用
    1.手机
    2.电气
    3.退出
    ''')
    a = int(input('>>'))
    if a in range(1, 4):
        if a == 1:
            shouji()
            if loggin_status is False:
                print('loggin_status为False')
            else:
                print('loggin_status为Ture')
        elif a == 2:
            dianqi()
        elif a == 3:
            break
    else:
        print('不再范围内....')
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-15 08:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表