如何制作一张有13张扑克牌的牌

时间:2017-07-06 18:43:37

标签: python

我接受了一个计算机科学项目,但我无法弄清楚如何将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))

1 个答案:

答案 0 :(得分:0)

当你这样做时

result = (self.pop())
在handPrinter方法中

也许你打算做

result = (self.x.pop())

执行代码时出错。另外,在Card类的__init__方法中,您应该识别self.suit = suitself.value = value

除此之外,添加

h = hand()
h.handPrinter()
最后,一切似乎对我都很好。