21koizyd
21koizyd

Reputation: 1993

Append object to array

I'm beginner with Python. I know only C++/C# so... My problem is append to my array some object which is Card with 2 parameters, card has color and value

part of code doesn't work

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

function throwCard() in Player

def throwCard(self):
    cardToThrow = self.setOfCards[0]
    del self.setOfCards[0]
    return cardToThrow

"main"

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

Do you have some suggestions?

AttributeError: 'list' object has no attribute 'throwCard'

Upvotes: 0

Views: 138

Answers (2)

Leonard2
Leonard2

Reputation: 890

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

this play method requires arguments to be flat, not giving a list explicitly. I mean, you should ...

# your main
game.play(player1, player2)

check this SO post.

Upvotes: 1

Rayhane Mama
Rayhane Mama

Reputation: 2424

try changing:

def play():

to:

def play(self,players):

should do it.

Upvotes: 2

Related Questions