在 python 中创建扑克牌手分析器
Creating a Poker Hand Analyzer in python
所以,我正试图从我的 python class 中抽出一个 C,但我的期末项目快要死了。首先,我们必须使用字典和列表来创建一副纸牌。我让它工作了。
suits = {0: "Hearts", 1: "Diamonds", 2: "Clubs", 3: "Spades"}
ranks = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
card_deck = []
def create_deck():
for suit in suits:
for rank in ranks:
card_deck.append([rank, suit])
create_deck()
现在我必须创建一个函数来将列表条目与实际卡片相关联。因此,1、0 将读作红心 A 等等。我不知道该怎么做。有人有任何建议或可能的链接吗?我们得到提示:
card = (1,0) 索引元组得到花色
suit=card[1] 现在取花色并将其插入字典以获取适当的文本
suit_text=套装[套装]
看来我需要创建一个新字典,里面有面卡
cards = {1: "Ace", 11: "Jack", 12: "Queen", 13: "King"}
但是我如何让它们关联到列表条目??
经过一番努力,我想通了。
def print_card():
for card in card_deck:
suit = card[1]
suit_text = suits[suit]
if card[0] == 1 or card[0] == 11 or card[0] == 12 or card[0] == 13:
rank = card[0]
rank_text = face_cards[rank]
else:
rank = card[0]
rank_text = ranks[rank] - 1
print (str(rank_text) + " of " + suit_text)
print_card()
所以,我正试图从我的 python class 中抽出一个 C,但我的期末项目快要死了。首先,我们必须使用字典和列表来创建一副纸牌。我让它工作了。
suits = {0: "Hearts", 1: "Diamonds", 2: "Clubs", 3: "Spades"}
ranks = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
card_deck = []
def create_deck():
for suit in suits:
for rank in ranks:
card_deck.append([rank, suit])
create_deck()
现在我必须创建一个函数来将列表条目与实际卡片相关联。因此,1、0 将读作红心 A 等等。我不知道该怎么做。有人有任何建议或可能的链接吗?我们得到提示:
card = (1,0) 索引元组得到花色 suit=card[1] 现在取花色并将其插入字典以获取适当的文本 suit_text=套装[套装]
看来我需要创建一个新字典,里面有面卡
cards = {1: "Ace", 11: "Jack", 12: "Queen", 13: "King"}
但是我如何让它们关联到列表条目??
经过一番努力,我想通了。
def print_card():
for card in card_deck:
suit = card[1]
suit_text = suits[suit]
if card[0] == 1 or card[0] == 11 or card[0] == 12 or card[0] == 13:
rank = card[0]
rank_text = face_cards[rank]
else:
rank = card[0]
rank_text = ranks[rank] - 1
print (str(rank_text) + " of " + suit_text)
print_card()