Skip to content Skip to sidebar Skip to footer

Creating A Playing Card Class Python

I created a playing card object that has certain attributes of rank, suite and blackjack value. The blackjack value part keep getting a TypeError that says I can't compare between

Solution 1:

Since you're a beginner. Let's consider the error it's giving you:

    if self.ranks > 1 or self.ranks < 11:
TypeError: '>' not supported between instances of 'list' and 'int'

There's a typo in your bjValue function:

if self.ranks > 1 or self.ranks < 11:
    self.value = self.ranks

You're comparing to the ranks list, instead of the actual (current) rank value.

What you meant was probably:

if self.rank > 1 or self.rank < 11:
    self.value = self.ranks[self.rank]

Edit

Based on your comment, what I suspect you want is something like this.

class Card:
    def __init__(self, rank, suit):
        self.ranks = [None, "Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
        self.rank = self.ranks[rank]
        self.suits = {"d": "Diamonds", "c": "Clubs", "h": "Hearts", "s": "Spades"}
        self.suit = self.suits[suit]
        self.value = -1

    def getRank(self):
        return self.rank

    def getSuit(self):
        return self.suit

    def bjValue(self):
        if self.rank == "Ace":
            self.value = 1
        elif self.rank == "Jack" or self.rank == "Queen" or self.rank == "King":
            self.value = 10
        elif type(self.rank) is int:
            if self.rank > 1 and self.rank < 11:
                self.value = self.ranks[self.rank]
        return self.value

Unless you want to be able to change the rank or suit on any already created cards, then I would recommend moving the if statements from bjValue into __init__.


Post a Comment for "Creating A Playing Card Class Python"