creating a playing card Class Python

荒凉一梦 提交于 2019-12-31 07:07:12

问题


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 instances of list and int. I'm not sure how to resolve (how to/if I can compare just the index of the list and then give it the blackjack value based on its index)?

Does the if/elif block belong within the bjValue() function or in the init() function?

Here's my code:

class Card:

    """playing card object where numeric rank, suit and blackjack values are stored"""

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




    def getRank(self):
        """returns the rank of the card"""
        return self.rank

    def getSuit(self):

        return self.suits.get(self.suit)

    def bjValue(self):

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

        elif self.rank == "Ace":
            self.value = 1

        elif self.rank == "Jack" or self.ranks == "Queen" or self.ranks == "King":
            self.value = 10


        return self.value


    def __str__(self):

        return "%s of %s" % (self.ranks[self.rank], self.suits.get(self.suit))





c1 = Card(1,"s")
c2 = Card(11,"d")
print(c1) #expect Ace of Spades
print(c2) #expect Jack of Diamonds

c3 = Card(5,"c") #expect 5 of clubs
print(c3.getRank())
print(c3.getSuit()) #expect Five
print(c3.bjValue()) #expect 5
print(c3)

c4 = Card(1,"s")
print(c4)

Edit: So, I fixed my typo and I'm not getting anymore errors but I'm still getting the incorrect blackjack value for the Jack, Queen and King cards (I get their blackjack value back as their rank even though i have the if statement) and I don't understand where the flaw in my logic is...


回答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__.



来源:https://stackoverflow.com/questions/42801071/creating-a-playing-card-class-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!