Method is not appending to list like other similar methods

87 Views Asked by At

I'm a beginner at pyhton. i'm working on a poker game. currently i'm stuck at dealing every player two cards. dealing the flop, turn and river work fine but dealing the player cards doesn't work. I don't understand why it doesn't work. Any solutions, explanation, other approaches advice are appreciated. Link to my github with the full code Github

    def deal(self, location, times=1): 
        for i in range(times):
            location.cards.append(self.pop(0)) 
    def deal_hole(self):
        count = 0
        while count < 2:
            for player in self.players_not_out:
                self.deck.deal(player, 1)
            count += 1

The Deal_hole method should append two cards to all the player class instances at the attribute cards which is a list, but it returns a empty list. i used the following code to test the method

list_of_players = [player('a'), player('b'), player('c'), player('d')]

game = game('q')
deck = deck()

a = player('a')
b = player('b')
c = player('c')
d = player('d')

game.shuffle_deck()
game.deal_hole()

player.show_hand(a,a)

This returned: a's hand: []

1

There are 1 best solutions below

2
yeti On

In the code you use for testing, by doing the following:

a = player('a')

you do not assign player('a') from list_of_players to variable a, but create another player with name "a". That is why you cannot see its cards. But it will work properly, when you do it like this:

a = list_of_players[0]
a.show_hand("a")

And speaking about good practices, you should not pass the player's name in the show_hand() method, because it takes self.name for printing, not the name you pass. So it's not used and its presence there is counterintuitive.

Apart from that:

  1. show_name() should return the string, not print it.
  2. You don't have to inherit after object like class Player(object):, this means the same: class Player:. The inheritance is implicit.
  3. You should pass list_of_players to Game's __init__ method, not intialize it outside and use that variable.
  4. Classes should be named starting with a capital letter (CapCase, CamelCase), according to Python Style Guide.

...and some more.

You can get your code's review on Code Review Stack Exchange.