-
写一个装饰器@tag要求满足如下功能:
@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>
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='h1') def render(text): # 执行其他操作 return text print(render('hello'))
-
为函数写一个装饰器,根据参数不同做不同操作。
flag为 True,则 让原函数执行后返回值加 100,并返回。 flag为 False,则 让原函数执行后返回值减 100,并返回。
def operation(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
为函数写一个装饰器,根据不同的运算符和数据对原函数的返回值进行相应的数值操作
例如: 运算符是+,数据是 100, 就对原函数的返回值进行加100的处理 运算符是-, 数据是 20,就对原函数的返回值进行减20的处理 运算符是*, 数据是 3, 就对原函数的返回值进行乘3的处理 运算符是/, 数据是 2, 就对原函数的返回值进行除2的处理 其他情况的符号不做处理
def handle(operator: str, num): def test(func): def new_func(*args, **kwargs): result = func(*args, **kwargs) if type(result) not in (int, float, complex): return result if operator == '+': return result + num elif operator == '-': return result - num elif operator == '*': return result * num elif operator == '/': return result / num else: return result return new_func return test
-
写一个斗地主发牌器
import random
str_d = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'Q', 'K', 'A']
str_z = ['大王', '小王']
str1 = []
str2 = []
str3 = []
str_dz = str_z + str_d * 4
str_dz1 = str_dz
x1 = 17
x2 = 17
x3 = 20
n1 = n2 = n3 = ''
n = 0
m = 53
while n < 54:
for x in range(0, x1):
n1 = str_dz1[random.randint(0, m)]
str1.append(n1)
str_dz1.remove(n1)
x1 -= 1
n += 1
m -= 1
break
for x in range(0, x2):
n2 = str_dz1[random.randint(0, m)]
str2.append(n2)
str_dz1.remove(n2)
x2 -= 1
n += 1
m -= 1
break
for x in range(0, x3):
n3 = str_dz1[random.randint(0, m)]
str3.append(n3)
str_dz1.remove(n3)
x3 -= 1
n += 1
m -= 1
break
print(str1)
print(str2)
print(str3)
import random
import time
str_d = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'Q', 'K', 'A']
str_z = ['大王', '小王']
str_h = ['♥', '♠', '♣', '♦']
str_dz = []
for h in str_h:
for d in str_d:
hd = h + d
str_dz.append(hd) # 制造扑克牌
str_dz = str_dz + str_z
z = 53
def fp(f):
str_dz1 = str_dz
str_scq = []
for x in range(0, f):
global z
n = str_dz1[random.randint(0, z)] # 随机取牌
str_scq.append(n) # 发牌到容器
str_dz1.remove(n) # 减去已发牌
z -= 1
return str_scq # 返回发牌容器
print("开始发牌:", "\n",fp(17), "\n", fp(17), "\n", fp(17), "\n", "地主牌:", fp(3))
from random import shuffle
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)
# key = "添加一个需要引用的函数规则先对列表数据进行处理,然后输出"
# 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())