方法一:
import random as r numberOfCard=52 numberOfPeople=4 types=['♥','♠','♦','♣'] symbols=['A','2','3','4','5','6','7','8','9','10','J','Q','K'] #生成1副扑克 pokers= {} for i in range(numberOfCard): pokers[i]=[types[i//13],symbols[i % 13]] #pokers[numberOfCard +1] = ['小', '王'] #pokers[numberOfCard +2] = ['大', '王'] #准备numberOfPeople人数的玩家 players= {} for i in range(numberOfPeople): players[i]=[] #发牌 给卡片编号,并从这些卡号随机抽取出来发给玩家 card=[i for i in range(numberOfCard)] for i in range(numberOfCard): c=r.choice(card) card.remove(c) players[i % numberOfPeople].append(pokers[c]) #显示玩家手中的牌 for i in range(numberOfPeople): print('第',i+1,'位牌手:',players[i])方法二:
import random as r numberOfPeople=4 types=['♥','♠','♦','♣'] symbols=['A','2','3','4','5','6','7','8','9','10','J','Q','K'] #洗牌 pokers=[i+j for i in types for j in symbols] r.shuffle(pokers) #准备numberOfPeople人数的玩家 players= {} for i in range(numberOfPeople): players[i]=[] #发牌 for i in range(len(pokers)): players[i % numberOfPeople].append(pokers[i]) #显示各位玩家的牌 for i in range(numberOfPeople): print('第',i+1,'位牌手:',players[i])方法三:
import random as r numberOfPeople=4 types=['♥','♠','♦','♣'] symbols=['A','2','3','4','5','6','7','8','9','10','J','Q','K'] #洗牌 pokers=[] ''' for i in types: for j in symbols: pokers.append([i,j]) ''' pokers=[[t,s]for t in types for s in symbols] r.shuffle(pokers) #发牌 ''' players=[] pokerOfEachPeople=len(pokers)//numberOfPeople for i in range(numberOfPeople): players.append(pokers[pokerOfEachPeople*i:pokerOfEachPeople*(i+1)]) print('牌手',i+1,':',players[i]) ''' pokerOfEachPeople=len(pokers)//numberOfPeople players=[pokers[pokerOfEachPeople*i:pokerOfEachPeople*(i+1)]for i in range(numberOfPeople)] #显示各位玩家的牌 for i in range(numberOfPeople): print('牌手',i+1,':',players[i])