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:

ifself.ranks > 1orself.ranks < 11:
TypeError: '>' not supported between instances of 'list'and'int'

There's a typo in your bjValue function:

ifself.ranks > 1orself.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:

ifself.rank > 1orself.rank < 11:
    self.value = self.ranks[self.rank]

Edit

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

classCard:
    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 = -1defgetRank(self):
        return self.rank

    defgetSuit(self):
        return self.suit

    defbjValue(self):
        if self.rank == "Ace":
            self.value = 1elif self.rank == "Jack"or self.rank == "Queen"or self.rank == "King":
            self.value = 10eliftype(self.rank) isint:
            if self.rank > 1and 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"