我正在尝试编写一个具有以下功能的程序:
一种创建一副牌的功能,默认为52张,或者当提供套装时仅为13张。
和
一个函数,用于从牌组中删除第一张cardcount牌,将它们作为列表返回,并在没有牌时返回适当的错误。
预期结果如下。
>>> deck2 = Deck('♠')
>>> deck2.shuffle_deck()
>>> print(deck2.cards)
[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠, J of ♠, 9 of ♠, Q of ♠, 6 of ♠, 2 of ♠, K of ♠]
>>> deck2.deal_card(7)
[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠]
>>> deck2.deal_card(7)
Cannot deal 7 cards. The deck only has 6 cards left!
我的问题是每次运行代码时都会返回一个空列表。我想我已经设置好了第一个类(PlayingCard(,并选择了适合的参数。然而,我不太确定如何实现交易功能,也不太确定为什么我的列表返回为空。我是不是错过了什么小东西?
import random
class PlayingCard():
def __init__(self, rank, suit):
acc_suit = ("♠", "♥", "♦", "♣")
acc_rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
assert rank in acc_rank, "Not a valid rank for a playing card."
assert suit in acc_suit, "Not a valid suit for a playing card."
self.suit = suit
self.rank = rank
def __repr__(self):
return self.rank + ' of ' + self.suit
class Deck():
def __init__(self, *suit):
acc_suit = ("♠", "♥", "♦", "♣")
self.cards = []
if suit == None:
for suit in range(4):
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
if suit in acc_suit:
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
def shuffle_deck(self):
random.shuffle(self.cards)
def deal_card(self):
return self.cards.pop(0)
def __str__(self):
res = []
for card in self.cards:
res.append(str(card))
return 'n'.join(res)
您的列表为空的原因是,如果您提供套装,您永远不会创建一套。调用
deck2 = Deck('♠')
将导致suit = ('♠',)
-一个1-元组(包含1个字符串(,因此它永远不会统计if suit in acc_suit:
=>self.deck
为空。
还有很多其他错误,使您的代码可以运行-在内联注释中指出:
import random
class PlayingCard():
def __init__(self, rank, suit):
# you try to create cards by range(2,14) but you assert that suit must
# be 2 to 10 or "J"-"A" for picture cards
# also: range(2,14) is [2..13] only so you miss a card
r = {11:"J",12:"Q",13:"K",14:"A"}
rank = r.get(rank,rank) # translate 10 to 14 to "JQMA"
acc_suit = ("♠", "♥", "♦", "♣")
acc_rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
assert rank in acc_rank, "Not a valid rank for a playing card."
assert suit in acc_suit, "Not a valid suit for a playing card."
self.suit = suit
self.rank = rank
def __repr__(self):
# can not add a string to an integer (rank is an int for 2-10 !)
return f"{self.rank} of {self.suit}"
和你的甲板等级:
class Deck():
# change this to a named param with default so you need to supply suit=' '
# omit the ,* if you do not like to force suit=" " when calling this
def __init__(self, *, suit = None):
acc_suit = ("♠", "♥", "♦", "♣")
self.cards = []
if not suit:
for suit in range(4):
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
if suit in acc_suit:
for rank in range (2,15):
card = PlayingCard(rank, suit)
self.cards.append(card)
def shuffle_deck(self):
random.shuffle(self.cards)
# this function does not accept parameters like you posted its usage
def deal_card(self, amount = 1):
# always returns a list of cards, even if you only request one
rv, self.cards = self.cards[:amount], self.cards[amount:]
# this does NOT do the evaluation of enough cards are left, do it yourself:
# if len(rv) != amount: raise whatever
return rv
def __str__(self):
return 'n'.join(str(c) for c in self.cards)
测试:
deck2 = Deck(suit = "♠")
print(deck2.cards)
deck2.shuffle_deck()
print(deck2.cards)
print(deck2.deal_card(7))
print(deck2.deal_card(7))
输出:
# orignal
[2 of ♠, 3 of ♠, 4 of ♠, 5 of ♠, 6 of ♠, 7 of ♠, 8 of ♠, 9 of ♠, 10 of ♠,
J of ♠, Q of ♠, K of ♠, A of ♠]
# shuffeled
[3 of ♠, 5 of ♠, 10 of ♠, 8 of ♠, A of ♠, 7 of ♠, 6 of ♠, 4 of ♠, J of ♠,
Q of ♠, 9 of ♠, 2 of ♠, K of ♠]
# first 7
[3 of ♠, 5 of ♠, 10 of ♠, 8 of ♠, A of ♠, 7 of ♠, 6 of ♠]
# next 7 (only 6 remain)
[4 of ♠, J of ♠, Q of ♠, 9 of ♠, 2 of ♠, K of ♠]