52张扑克牌发个4个玩家,每人13张。
要求:
自动生成一幅扑克牌组;洗牌;发牌到玩家手中;将玩家手中扑克牌按花色大小整理好。
思路一
import random
import operator
# 初始化扑克牌
def auto():
pokers=[]
for i in ['♥','♠','♦','♣']:
for j in ['A','2','3','4','5','6','7','8','9','10','J','Q','K']:
pokers.append(i+j)
return pokers
poker=auto()
# 洗牌
random.shuffle(poker)
li={}
for k in ['player1','player2','player3','player4']:
# 发牌
b=random.sample(poker,13)
for s in b:
poker.remove(s)
li.setdefault(k,b)
# 按花色大小整理扑克牌
print('player1:',sorted(li['player1'],key=operator.itemgetter(0,1)))
print('player2:',sorted(li['player2'],key=operator.itemgetter(0,1)))
print('player3:',sorted(li['player3'],key=operator.itemgetter(0,1)))
print('player4:',sorted(li['player4'],key=operator.itemgetter(0,1)))