将对象附加到数组

2024-04-18 10:11:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者。我只知道C++ +C,所以… 我的问题是append到我的数组中的某个对象是Card,有两个参数,卡片有颜色和值

部分代码不起作用

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' object has no attribute 'throwCard'


Tags: selfgameplaydeftableplayerplayersappend
2条回答
class Game:
    # ...
    def play(self, *players):
        # ...

这个play方法要求参数是平坦的,而不是显式地给出一个列表。我是说,你应该。。。你知道吗

# your main
game.play(player1, player2)

检查this SO post。你知道吗

尝试更改:

def play():

收件人:

def play(self,players):

我应该这么做。你知道吗

相关问题 更多 >