2019-08-05作业

1.写一个生成式能够产生的数据为: 1, 4, 27, 256, 3125,…, 9**9

nums=(x**x for x in range(1,10))
print(list(nums))#[1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]

2.写一个生成式能够产生1-10中所有半径是偶数的圆的面积

S=(x**2*math.pi for x in range(1,11) if not x%2)
print(list(S))
#[12.566370614359172, 50.26548245743669, 113.09733552923255, 201.06192982974676, 314.1592653589793]

3.写一个生成式交换字段的键和值,产生一个新的字典

dict1={"a":1,"b":4,"c":5}
change_key=dict((value,key) for key,value in dict1.items())
print(change_key)#{1: 'a', 4: 'b', 5: 'c'}

4.为函数写一个装饰器,在函数执行之后输出 after

def add_after(fn):
    def inner(*args,**kwargs):
        fn(*args,**kwargs)
        print("After")
    return inner
@add_after
def print_alpha():
    print("asdfgg")

print_alpha()

#asdfgg
#After

5.为函数写一个装饰器,把函数的返回值 +100 然后再返回。

def add_hundred(fn):
    def inner(*args,**kwargs):
        fn(*args, **kwargs)
        return args[0]+100
    return inner

@add_hundred
def print_num(x):
    return x

print(print_num(67))
#167

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

def tag(fn):
    def inner(*args,**kwargs):
        return "

"+fn(*args,**kwargs)+"

" return inner @tag def render2(): return 'abc' @tag def render(text): return text print(render2())#

abc

print(render("Hello"))#

Hello

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


image.png
def tag(name):
    def outer(fn):
        def inner(*args, **kwargs):
            return "<{0}>{1}<{0}>".format(name, fn(*args))
        return inner
    return outer


@tag(name="p")
def render(text):
    return text


print(render("hello"))#

hello

@tag(name='div') def render2(): return 'abc' print(render2())#

abc

7.为函数写一个装饰器,根据参数不同做不同操作。
flag为True,则 让原函数执行后返回值加100,并返回。
flag为False,则 让原函数执行后返回值减100,并返回。

def fun(flag):
    def outer(fn):
        def inner(*args, **kwargs):
            if flag == True:
                return fn(*args) + 100
            else:
                return fn(*args) - 100

        return inner

    return outer


@fun(flag=True)
def print_num(x):
    return x


print(print_num(100))


@fun(flag=False)
def print_num(x):
    return x


print(print_num(100))

8.写一个斗地主发牌器

Cards = set()
for hua in ["♤", "♡", "♢", "♧"]:
    for num in ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]:
        Cards.add(str(hua) + str(num))
Cards.add("JOKER")
Cards.add("Joker")
print(len((list(Cards))))
iter1 = iter(Cards)
cards_list1 = []
cards_list2 = []
cards_list3 = []


def Shuffle_the_deck(farmer1_cards, farmer2_cards, landlord_cards):
    for i in range(17):
        farmer1_cards()
        farmer2_cards()
        landlord_cards()

def farmer1_cards():
    cards_list1.append(next(iter1))
    return cards_list1


def farmer2_cards():
    cards_list2.append(next(iter1))
    return cards_list2


def landlord_cards():
    cards_list3.append(next(iter1))
    return cards_list3

Shuffle_the_deck(farmer1_cards, farmer2_cards, landlord_cards)

print("农名1的牌:", cards_list1)
print("农名1的牌:", cards_list2)
excess_cards = []
excess_cards.append(next(iter1))
excess_cards.append(next(iter1))
excess_cards.append(next(iter1))
print("抓地主:",excess_cards)
print("地主的牌:" , cards_list3 + excess_cards)
#农名1的牌: ['♤7', '♧4', '♤10', '♧2', '♧5', '♢7', '♧6', '♤3', '♢8', '♤4', '♡5', '♡K', '♢10', '♡2', '♤Q', '♤5', '♤8']
#农名2的牌: ['Joker', '♢4', '♡4', '♧Q', '♡Q', '♤9', '♧A', '♢A', '♧7', '♢K', '♢3', '♢5', '♡6', '♧9', '♧K', '♧10', '♤2']
#抓地主: ['♧J', '♢J', '♤6']
#地主的牌: ['JOKER', '♤J', '♡7', '♡J', '♤A', '♢9', '♢6', '♡9', '♢Q', '♡10', '♧8', '♤K', '♧3', '♡3', '♢2', '♡A', '♡8', '♧J', '♢J', '♤6']

你可能感兴趣的:(2019-08-05作业)