class Card:
RANKS = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Jack', 'Queen', 'King']
def __init__(self, rank, suit):
self.suit = suit
self.rank = rank
def get_name(self):
return Card.RANKS[self.rank - 1] + ' of ' + self.suit
def equals(self, other_card):
if self.rank == self.other_card:
return True
elif self.rank != self.other_card:
return False
def greater_than(self, other_card):
if self.rank > self.other_card:
return True
elif self.rank < self.other_card:
return False
def less_than(self, other_card):
if self.rank < self.other_card:
return True
elif self.rank > self.other_card:
return False
class Deck:
def __init__(self):
self.available_cards = []
self.dealt_cards = []
for suit in ['Hearts', 'Spades', 'Diamonds', 'Clubs']:
for rank in range(1, 14):
self.available_cards.append(Card(rank, suit))
def shuffle(self):
"""
Shuffles the cards in the deck.
"""
import random
return random.shuffle(self.available_cards)
def deal_card(self):
self.available_cards.add(Card)
self.available_cards.remove(Card)
self.dealt_cards.append(Card)
def get_size(self):
return len[self.available_cards]
c = Card()
print(c.available_cards)
d = Deck()
d.shuffle()
print(d.dealt_cards)
所以我在这里所做的是我已经制作了一个Card类和一个Deck类,但是我在执行它时遇到了麻烦。我试图通过改组它来测试这两个类,然后用它来处理一些卡片。但是,当我尝试运行它将无法正常工作。我该怎么办?
答案 0 :(得分:1)
肯定有一个问题:
c = Card()
print(c.available_cards)
因为1)Card
类的构造函数是否有2个参数(rank
和suit
和2),所以Cards类没有available_cards
,所以试图访问c.available_cards
将无效
所以不知道你对这两行的意图我省略了进一步的调试。
现在你在做:
d = Deck()
好的,这不需要任何参数,并创建一个包含标准52牌组中所有牌的新牌组,并将dealt_cards
初始化为[]
。
现在你立即打印
print(d.dealt_cards)
当然会产生一个空列表:
print(d.dealt_cards)
>>>[]
你可能想要的可能是首先处理一些卡然后打印结果:
for i in range(4):
d.deal_card()
print(d.dealt_cards)
但是这会导致错误消息,因为deal_card
不正确。您不想向available_cards
添加任何内容,但会从中删除一项并将其添加到dealt_cards
,如下所示:
def deal_card(self):
# self.available_cards.add(Card)
# self.available_cards.remove(Card)
self.dealt_cards.append(self.available_cards.pop())
现在您会注意到print(d.dealt_cards)
没有以“漂亮”的方式输出列表中的卡片,而是您可能会看到如下内容:
[<__main__.Card object at 0x00000285E8C0A978>, <__main__.Card object at 0x00000285E8C0AA58>, <__main__.Card object at 0x00000285E8C0A4E0>, <__main__.Card object at 0x00000285E8BFCF28>]
那是因为你没有教你的脚本如何打印卡片。为此,我们向Cards类添加一个方法:
def __repr__(self):
return self.get_name()
现在每当打印一个卡片类的实例时,我们都会得到所需的输出。
最终代码:
class Card:
def __init__(self, rank, suit):
self.suit = suit
self.rank = rank
self.ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', 'Jack', 'Queen', 'King']
def get_name(self):
return Card.ranks[self.rank - 1] + ' of ' + self.suit
def equals(self, other_card):
if self.rank == other_card.rank:
return True
elif self.rank != other_card.rank:
return False
def greater_than(self, other_card):
if self.rank > other_card.rank:
return True
elif self.rank < other_card.rank:
return False
def less_than(self, other_card):
if self.rank < other_card.rank:
return True
elif self.rank > other_card.rank:
return False
def __repr__(self):
return self.get_name()
class Deck:
def __init__(self):
self.available_cards = []
self.dealt_cards = []
for suit in ['Hearts', 'Spades', 'Diamonds', 'Clubs']:
for rank in range(1, 14):
self.available_cards.append(Card(rank, suit))
def shuffle(self):
"""
Shuffles the cards in the deck.
"""
import random
return random.shuffle(self.available_cards)
def deal_card(self):
# self.available_cards.add(Card)
# self.available_cards.remove(Card)
self.dealt_cards.append(self.available_cards.pop())
def get_size(self):
return len[self.available_cards]
# c = Card()
# print(c.available_cards)
d = Deck()
d.shuffle()
for i in range(4):
d.deal_card()
print(d.dealt_cards)
>>>[6 of Spades, Jack of Hearts, 4 of Spades, Queen of Spades]