将对象追加到数组

时间:2017-06-30 13:40:34

标签: python arrays

我是Python的初学者。我只知道C ++ / C#所以...... 我的问题是append我的数组中有一个对象Card有2个参数,卡有颜色和值

部分代码无效

class Game: 
    table = []
    ....
    def play(self, *players):
        for singlePlayer in players:
            self.table.append(singlePlayer.throwCard())

throwCard()

中的Player功能
def throwCard(self):
    cardToThrow = self.setOfCards[0]
    del self.setOfCards[0]
    return cardToThrow

"主"

player1 = Player()
player2 = Player()
game = Game()
game.play([player1, player2])

你有什么建议吗?

  

AttributeError:' list'对象没有属性' throwCard'

2 个答案:

答案 0 :(得分:2)

尝试改变:

def play():

为:

def play(self,players):

应该这样做。

答案 1 :(得分:1)

class Game:
    # ...
    def play(self, *players):
        # ...

这个play方法要求参数是平的,而不是明确给出列表。我的意思是,你应该......

# your main
game.play(player1, player2)

检查this SO post