【问题标题】:Class doesn't recognize the attribute类无法识别该属性
【发布时间】:2023-03-10 14:07:01
【问题描述】:
class Hand:

    def __int__(self):
        self.value= 0
        self.ace=False
        self.Cards = []

    def __str__(self):
        hand_comp=""
        for card in self.Cards:
            card_name=card.__str__()
            hand_comp+= " " + card_name

            return 'The card has %s' %(hand_comp)

    def card_add(self, card):
        '''Add another card to the hand'''
        self.Cards.append(card)

每次运行时,我都会收到一条错误消息,提示“对象'手'没有属性'卡片'。我该如何纠正这个问题?

【问题讨论】:

  • 初始化方法是__init__,不是__int__
  • 请注意,您可以一次性为__str__ 构建卡片串:handcomp = " ".join(str(card) for card in self.Cards)。这也避免了不必要的前导空间。
  • 非常感谢你们!它有很大帮助。这是一个愚蠢的错误! :)

标签: python class oop object


【解决方案1】:

您在初始化类时使用了错误的关键字 它应该是 init 而不是 int

def __init__(self):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2014-08-10
    相关资源
    最近更新 更多