1

I am working on a texas hold-em game in python, and am looking to traverse an array containing a complete hand of 7 cards (2 in the hole + 5 on the board). The array contains elements of class Cards, where the Card class constructor is

class Card:
    def __init__(self, suit, val):
        self.suit = suit
        self.value = val

So, I have a "hand" array within a "Player" class of 7 random cards, where the suit is one of 4 strings (spade, club, heart, diamond) and the value is one of 9 numbers (2-10) or 4 strings (jack-ace). I want to traverse the array to check if the list contains any of the hands in poker, but I'm failing to figure out how I can "extract" the suit/value of the cards from my array. I've started a method within my "Player" class to check for a suit, here that suit is a spade.

class Player:
    def __init__(self, name):
        self.name = name
        self.pocket = []
        self.hand = []

    def spadeChecker(self):
        card = Card("Blank", 0)
        for i in self.hand:
            card = self.hand[i]
            if(card.suit == "Spade"):
                print("Hi! you have a spade!")
            else:
                pass

When running the program from my terminal I receive a TypeError message:

in spadeChecker card = self.hand[i] TypeError: list indices must be integers or slices, not Card

I know my method is pretty bad but I'm very new to this and just can't figure out how to get it to work. Any advice?

Thanks

  • Good description, but your code is missing some classes like Card. Please show a working piece of code you wrote so we can copy paste and run it and answer more easily. See MRE here: https://stackoverflow.com/help/minimal-reproducible-example – Malo Mar 09 '21 at 07:03

3 Answers3

2

Here is the rewritten method, including an example with a 3 card hand.

class Card:
    def __init__(self, suit, val):
        self.suit = suit
        self.value = val

class Player:
    def __init__(self, name):
        self.name = name
        self.pocket = []
        self.hand = [Card("Heart", 10), Card("Spade", 10), Card("Diamond", 10)]

    def spadeChecker(self):
        for card in self.hand:
            if(card.suit == "Spade"):
                print("Hi! you have a spade!")
                #return True
            else:
                #return False
                pass

p = Player("Bob")
p.spadeChecker()

Output is:

Hi! you have a spade!
Malo
  • 1,233
  • 1
  • 8
  • 25
1

In your code, your iterator is i and your iterable is self.hand, which is a list of Card objects. Thus, i will be a Card object each time the loop iterates. If you want to be able to get the index of the Card object as well as the Card object itself, I recommend using the enumerate() function. However, since you only reference the Card objects, you can just get the suit attribute directly.

class Player:
    def __init__(self, name):
        self.name = name
        self.pocket = []
        self.hand = []

    def spadeChecker(self):
        card = Card("Blank", 0)
        for card_obj in self.hand:
            if(card_obj.suit == "Spade"):
                print("Hi! you have a spade!")
            else:
                pass
Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
0

There's a Python library for generating, comparing & evaluating poker hands called tilted which can be found here: https://github.com/MaxAtkinson/tilted

I've used it myself before and it's really easy. This may allow you to avoid having to implement it yourself.

psilocybin
  • 1,070
  • 7
  • 25