我得到了一个计算机科学的项目,但是我不知道如何把13张卡片拿到手里。在hand类中有一个内存错误,但是我不知道为什么,它不是每只手放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))
当你这么做的时候
用手印的方法也许你想
在执行代码时导致错误。另外,在Card类的
__init__
方法中,应该识别self.suit = suit
和self.value = value
。你知道吗除此之外,通过添加
最后,我觉得一切都很顺利。你知道吗
相关问题 更多 >
编程相关推荐