Python学习第125天(练习记录)

今天有点懒,就复习一下以前的内容,然后算是休息一天吧。

一、计算器(正则部分的复习)

import re
def hefa(test):
    l_kuohao = re.findall('(',test)
    # print(len(l_kuohao))
    r_kuohao = re.findall(')',test)
    # print(len(r_kuohao))
    zimu = re.findall('[a-z|A-Z]+',test)
    # print(len(zimu))
    xiaoshudian = re.findall(r'..',test)
    # print(len(xiaoshudian))
    if len(l_kuohao) ==len(r_kuohao) and len(zimu) == 0 and len(xiaoshudian) == 0:
        return True
    else:
        return False

def input_1():
    while True:
        test = input('请输入计算内容>>>')
        if hefa(test) == True:
            break
        else:
            print('请重新输入')
    return test

def cheng(test):
    test = test.replace('--','+')
    test = test.replace('+-','-')
    test = test.replace(' ', '')
    while True:
        if len(re.findall('[*/]',test)) != 0 :
            test_chchu = re.search('-?d+.?d*[*/]-?d+.?d*',test).group()
            if len(re.split('*',test_chchu)) == 2:
                test_ji =float(re.split('*',test_chchu)[0].strip()) * float(re.split('*',test_chchu)[1].strip())
                test = test.replace(test_chchu,str(test_ji))
            else:
                test_chu = float(re.split('/',test_chchu)[0].strip()) / float(re.split('/',test_chchu)[1].strip())
                test = re.sub(test_chchu,str(test_chu),test)
        else:
            break
    return test

def add_(test):
    test = test.replace('--','+')
    test = test.replace('+-','-')
    test = test.replace(' ','')
    test_list = re.findall('-?d+.?d*',test)
    a = 0
    for i in test_list:
        a += float(i)
    test = str(a)

    # while True:
    #     # print(len(re.findall('-?d+.?d*[+-]-?d+.?d*',test)))
    #     if len(re.findall('-?d+.?d*[+-]-?d+.?d*',test)) != 0 :
    #         test_jiajian = re.search('-?d+.?d*[+-]-?d+.?d*',test).group()
    #         if len(re.split('+',test_jiajian)) == 2:
    #             test_he =float(re.split('+',test_jiajian)[0].strip()) + float(re.split('+',test_jiajian)[1].strip())
    #             test = test.replace(test_jiajian,str(test_he))
    #         else:
    #             test_cha = float(re.split(r'-',test_jiajian)[0].strip()) - float(re.split(r'-',test_jiajian)[1].strip())
    #             test = re.sub(test_jiajian,str(test_cha),test)
    #     else:
    #         break
    return test


def kuohao(test):
    # print(type(test),test)
    test_old = re.search('([^()]*)',test).group()
    # print(type(test_old),test_old)
    test_old_lone = test_old.replace('(', '')
    # print(test_old_lone)
    test_old_lone = test_old_lone.replace(')', '')
    # print(test_old_lone)
    a = cheng(test_old_lone)
    # print(a)
    test_new = add_(a)
    # print(type(test_new),test_new)
    test = test.replace(test_old, test_new)
    # print(test)
    return test


if __name__ == '__main__':
    # test = input_1()
    test = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
    test = test.replace(' ','')
    print(test)
    while True:
        if len(re.findall('(',test)) != 0 :
            test = kuohao(test)
            print(test)
        else:
            break
    a = cheng(test)
    # print(type(a),a)
    test_new = add_(a)
    print(type(test_new),test_new)
    print(eval('1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'))
    if test_new == eval(test):
        print(test_new)

二、关于修饰器(京东登录界面操作)

user_list=[
    {'name':'alex','passwd':'123'},
    {'name':'zhoujielun','passwd':'123'},
    {'name':'zhaolei','passwd':'123'},
    {'name':'wenzhi','passwd':'123'},
]
current_dic={'username':None,'login':False}


def choice_onload(how_to = None):
    def onload(func):
        def wrapper(*arg,**kwargs):
            if how_to =='zhuye':
                print('直接进吧')
                res = func()
                return res
            else:
                if current_dic['username'] and current_dic['login']:
                    res = func()
                    return res
                else:
                    a = 1
                    while a < 4:
                        name = input('请输入用户名>>>')
                        password = input('请输入密  码>>>')
                        for user in user_list:
                            if name == user['name'] and password == user['passwd']:
                                current_dic['username'] = user['name']
                                current_dic['login'] = True
                                res = func()
                                return res
                        else:
                            print('密码错误,请重新输入')
                        a += 1
                    if a == 4:
                        return '多次密码输入错误,你的账号被锁定'
                    else:
                        pass
        return wrapper
    return onload

@choice_onload
def dingdan():
    print('欢迎进入个人订单')
@choice_onload
def gouwuche():
    return ('欢迎进入购物车')
@choice_onload('zhuye')
def zhuye():
    return ('欢迎进入首页')


print(dingdan())  #这个时候就可以实现直接进入,不用输入密码

修饰器这部分估计得是要回去看视频了,有点不理解了,可能得花一天。

原文地址:https://www.cnblogs.com/xiaoyaotx/p/13269809.html