-1
    from Card import Card


    class Hand(object):
        def __init__(self):
            self.cards=[]


        def takeCards(self, cards):
            self.cards.append(card)
            pass

        def clear(self):
            self.card=[]
            pass

        def getTotal(self):
            for card in slef.cards:
                if card.value >21 and card ==11:
                    return 1
                elif not card.value:
                    return None
                else:
                    total+=card.value        
            pass

        def isBusted(self):
            if getTotal>21:
                return True


        def __str__(self):
            if self.__cards:
                card = ""
                for i in self.__cards:
                    card += str(i) + "\t"
            else:
                card = "Empty"
            return card + "\t(value " + str(self.getTotal())+")"



from Hand import Hand


class Dealer():

    def __init__(self,name):
        super(Dealer,self).__init__() 
        self.name = name
        pass

    def takeCards(self,cards):
        self.__hand.takeCards(cards)
        pass

    def clearHand(self):
        self.Hand.clear(card)
        pass

我正在写一个关于二十一点的游戏,但我有一些无法修复的错误......我有两个班级。当我测试我的代码时,程序说,经销商对象没有属性“手”。在明确的手的定义。我不知道如何解决这个问题。

4

1 回答 1

1

我看到的第一个问题是您正在通过 self 访问 Hand。在您的 Dealer 类中,您没有实例化 Hand 类。您可以在 Dealer 课程中尝试以下操作。

class Dealer(object):

    def __init__(self,name):
        super(Dealer,self).__init__() 
        self.name = name
        self.__hand = Hand() # Instance Hand so you can use its contents.
        pass

    def takeCards(self,cards):
        self.__hand.takeCards(cards)
        pass

    def clearHand(self):
        # self.Hand.clear(card)
        self.__hand.clear(card) # fixed this to point to our instance
        pass
于 2013-02-25T04:17:57.557 回答