day11.作业

  1. 写一个生成式能够产生的数据为: 1, 2, 27, 256, 3125,…, 9**9
def pow_new():
    for num in range(1,10):
        yield num ** num

  1. 写一个生成式能够产生1-10中所有半径是偶数的圆的面积
def rect_circle():
    import math
    for num in range(1,11):
        if not num & 1:
            yield round(num**2*math.pi,2)
  1. 写一个生成式交换字段的键和值,产生一个新的字典
def dict_change(dict1):
    dict1 = dict((dict1[key],key) for key in dict1)
    return dict1

  1. 为函数写一个装饰器,在函数执行之后输出 after
def after(fun1):
    def inner(*args,**kwargs):
        fun1(*args,**kwargs)
        print('after')
    return inner
@after
def fun():
    print(123)

  1. 为函数写一个装饰器,把函数的返回值 +100 然后再返回。
def add(fun1):
    def inner(*args,**kwargs):
        value = fun1(*args,**kwargs)
        return value+100
    return inner
@add
def fun2():
    return 30
  1. 写一个装饰器@tag要求满足如下功能:

    @tag
    def render(text):
        # 执行其他操作
        return text
    
    @tag
    def render2():
        return 'abc'
    
    print(render('Hello'))   # 打印出: 

    Hello

    print(render2()) # 打印出:

    abc

def tag(fun1):
    def inner(*args,**kwargs):
        value = fun1(*args,**kwargs)
        return '

'+str(value)+'

' return inner
  1. 写一个装饰器@tag要求满足如下功能(需要使用带参的装饰器,自己先自学正在一下):

    @tag(name='p')
    def render(text):
        # 执行其他操作
        return text
    
    @tag(name='div')
    def render2():
        return 'abc'
    
    print(render('Hello'))   # 打印出: 

    Hello

    print(render2()) # 打印出:
    abc
def tag(name):
    def inner_first(func1):
        def inner_second(*args,**kwargs):
            value = func1(*args,**kwargs)
            return '<'+ name + '>'+ value + ''
        return inner_second
    return inner_first

  1. 为函数写一个装饰器,根据参数不同做不同操作。
    flag为True,则 让原函数执行后返回值加100,并返回。
    flag为False,则 让原函数执行后返回值减100,并返回。
def flag_value(flag):
    def inner_first(fun1):
        def inner_second(*args,**kwargs):
            value = fun1(*args,**kwargs)
            if flag:
                return value + 100
            else:
                return value - 100
        return inner_second
    return inner_first

  1. 写一个斗地主发牌器
def doudizhu():
    def poke_has():
        import random
        list1 = [str(x) for x in range(2, 11)] + ['j', 'Q', 'K', 'A']
        dict1 = {'♠': list1, '♥': list1, '♦': list1, '♣': list1}
        list2 = [key + value for key in dict1 for value in dict1[key]]
        list2 = list2 + ['大王', '小王']
        len_list = len(list2)
        for _ in range(len_list):
            poke = random.choice(list2)
            yield poke
            list2.remove(poke)
    game = poke_has()
    user1 = [next(game) for _ in range(17)]
    user2 = [next(game) for _ in range(17)]
    user3 = [next(game) for _ in range(17)]
    dizhu = input('请输入哪个用户是地主:(user1-3)')
    if dizhu == 'user1':
        user1 += [next(game) for _ in range(3)]
    elif dizhu == 'user2':
        user2 += [next(game) for _ in range(3)]
    else:
        user3 += [next(game) for _ in range(3)]
    return {'user1':user1},{'user2':user2},{'user3':user3}
# print(doudizhu())

你可能感兴趣的:(day11.作业)