我正在尝试创建一个程序,将一副纸牌(总共30张纸牌,每张纸牌的标签为1-10,并分配黄色,黑色和红色),然后由两名玩家选择卡的“顶部”。
到目前为止,我有这个:
class Card:
def __init__(self, value, color):
self.value = value
self.color = color
我如何精确“洗牌”然后让两名玩家选择前两张牌?
答案 0 :(得分:1)
尝试以下代码:
import random
class Card:
def __init__(self, value, color):
self.value = value
self.color = color
#Store all 30 cards in a list
cards=[]
for i in range(0,10):
cards.append(Card(i,"red"))
cards.append(Card(i,"black"))
cards.append(Card(i,"yellow"))
#Shuffle the deck
random.shuffle(cards)
#Get top card:
topCard=cards[-1]
del cards[-1]
print(str(topCard.value) + ", " + topCard.color)
玩家已移除的卡牌为topCard