day12 作业

  1. 写一个装饰器@tag要求满足如下功能:

    def tag(name):
        def test(func):
            def new_func(*args, **kwargs):
                result = func(*args, **kwargs)
                if type(result) != str:
                    return result
                return f'<{name}>{result}</{name}>'     
            return new_func
        return test
    
    
    @tag(name='p')
    def render(text):
        # 执行其他操作
        return text
    
    @tag(name='div')
    def render2():
        return 'abc'
    
    print(render('Hello'))   # 打印出: <p>Hello</p>
    print(render2())     # 打印出: <div>abc</div>
    
  2. 为函数写一个装饰器,根据参数不同做不同操作。

    flag为 True,则 让原函数执行后返回值加 100,并返回。
    flag为 False,则 让原函数执行后返回值减 100,并返回。
    
    def tag(flag):
        def test(func):
            def new_func(*args, **kwargs):
                result = func(*args, **kwargs)
                if type(result) not in (int, float):
                    return result
                if flag:
                    return result+100
                else:
                    return result-100
                
            return new_func
        return test
    
    
    @tag(flag=bool(150))
    def func1(x, y):
        return x*y
    
    
    print(func1(10, 20))
    
  3. 为函数写一个装饰器,根据不同的运算符和数据对原函数的返回值进行相应的数值操作

    例如:
    运算符是+,数据是 100, 就对原函数的返回值进行加100的处理
    运算符是-, 数据是 20,就对原函数的返回值进行减20的处理
    运算符是*, 数据是 3, 就对原函数的返回值进行乘3的处理
    运算符是/, 数据是 2, 就对原函数的返回值进行除2的处理
    其他情况的符号不做处理
    
    def operator(str, num):
        def test(func):
            def new_func(*args, **kwargs):
                result = func(*args, **kwargs)
                if str == '+' and type(result) in (int, float):
                    return result+num
                elif str == '-' and type(result) in (int, float):
                    return result-num
                elif str == '*' and type(result) in (int, float):
                    return result*num
                elif str == '/' and type(result) in (int, float) and num !=0:
                    return result/num
                return result
            return new_func
        return test
    
    
    @operator(str='+', num=100)
    def num1():
        return 100
    
    
    @operator(str='-', num=20)
    def num2():
        return 100
    
    
    @operator(str='*', num=3)
    def num3():
        return 100
    
    
    @operator(str='/', num=2)
    def num4():
        return 100
    
    
    print(num1())
    print(num2())
    print(num3())
    print(num4())
    
  4. 写一个斗地主发牌器

    # ♥A ♠2 ♣5 ♦6  JOCKER jocker
    def create_cards():
        # 创建牌
        colors = ['♥', '♠', '♣', '♦']
        nums = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
        cards = ['JOCKER', 'jocker']
        for num in nums:
            for color in colors:
                cards.append(color+num)
        # 洗牌
        shuffle(cards)
    
        return iter(cards)
    
    
    def deal_cards():
        # 1.准备一副牌
        # 2.洗牌(打乱牌的顺序)
        cards = create_cards()
    
        # 3.发牌
        player1 = []
        player2 = []
        player3 = []
        for _ in range(17):
            player1.append(next(cards))
            player2.append(next(cards))
            player3.append(next(cards))
    
        player1.sort(key=key, reverse=True)
        player2.sort(key=key, reverse=True)
        player3.sort(key=key, reverse=True)
    
        # 4.返回分配好的4组牌
        return player1, player2, player3, list(cards)
    
    
    def key(item):
        num = item[1:]
        table = {
          
          
            'J': 11,
            'Q': 12,
            'K': 13,
            'A': 14,
            '2': 15,
            'ocker': 16,
            'OCKER': 17
        }
        if '3' <= num <= '9' or num == '10':
            return int(num)
        return table[num]
    
    print(deal_cards())
    

猜你喜欢

转载自blog.csdn.net/bbbbbya/article/details/109093557