你玩过斗地主吗?四个人一起玩很有乐趣!那么你学会了Python可否用程序来制作一幅扑克牌模拟发牌给四位玩家呢?让我们来试试吧!
import random
types=['♣','♠','♦','♥']
num=['A','2','3','4','5','6','7','8','9','10','J','Q','K']
cards=[]
player_A=[]
player_B=[]
player_C=[]
player_D=[]
for x in range(4):
for y in range(13):
cards.append(types[x]+num[y])
random.shuffle(cards)
for i in range(len(cards)):
if 0<=i<13 :
player_A.append(cards[i])
if 13<=i<26:
player_B.append(cards[i])
if 26<=i<39 :
player_C.append(cards[i])
if 39<=i<53 :
player_D.append(cards[i])
print("洗好的牌:",cards)
print("第一个玩家的牌:",player_A)
print("第一个玩家的牌:",player_B)
print("第一个玩家的牌:",player_C)
print("第一个玩家的牌:",player_D)
print("开始玩起来吧!")
程序输出结果如下:
洗好的牌: ['♦2', '♣5', '♠7', '♠5', '♦<