-1
我被授予了一个计算机科学项目,但是我无法弄清楚如何获得13张牌进入我的手。手类有一个内存错误,但我不明白为什么,它不是每手放13张牌。这是我到目前为止的代码:如何创建一个有13张扑克牌的牌
#Keshav Ramesh
#Project 8
import random
class card:
def __init__(self, suit, value):
self.suit=suit
self.value=value
#methods
def printer(self):
print("{0} {1}".format(self.suit, self.value))
def __lt__(self, other):
if(self.value == other.value):
return self.suit < other.suit
elif (self.value != other.value):
return self.value < other.value
def __gt__(self, other):
return not(self<other)
def __str__(self):
return "Suit: " + str(self.suit) + " value: " + str(self.value)
class deck:
def __init__(self):
self.x=[]
self.load()
random.shuffle(self.x)
def load(self):
for i in range(1, 5):
for j in range(2, 14):
self.x.append(card(i, j))
def deal(self):
return self.x.pop()
p = deck()
class hand:
def __init__(self):
self.x=[]
self.hand_count=0
while len(self.x) != 13:
self.x.append(p.deal())
def accept(self, a):
self.x.append(a)
self.hand_count= self.hand_count + 1
def play(self):
self.hand_count = self.hand_count - 1
return self.x.pop()
def handPrinter(self):
while len(self.x) != 0:
result = (self.pop())
print("{0} {1}".format(result.suit, result.value))
'hand_count'似乎等同于'len(x)',所以你可以通过手动跟踪'hand_count'来节省一些麻烦。你可以用'self.x = [p.deal()for _ in range(13)]' – Will
'来简化'hand .__ init__'。我在运行这个时没有遇到错误。请编写代码,以便重现错误。 – kabanus
你什么时候尝试将卡片添加到你的手类中。我拿了你的代码并创建了一个手,并执行了'my.accept(p.deal())',并且将卡添加到我的手中。 –