创建纸牌类Python [英] creating a playing card Class Python

查看:89
本文介绍了创建纸牌类Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个扑克牌对象,该对象具有等级,套房和二十一点值的某些属性.

I created a playing card object that has certain attributes of rank, suite and blackjack value.

21点值部分不断收到TypeError,提示我无法在listint的实例之间进行比较.我不确定如何解决(如何/如果我可以只比较列表的索引,然后根据其索引为其提供二十一点值)?

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)?

if/elif块是属于bjValue()函数还是属于 init ()函数?

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

这是我的代码:

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)

因此,我更正了我的错别字,并且不再出现任何错误,但是我仍然得到了杰克,皇后和国王卡的不正确二十一点值(即使我有if语句),但我不知道我的逻辑缺陷在哪里...

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...

推荐答案

由于您是初学者.让我们考虑一下它给您的错误:

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'

bjValue函数中有一个错字:

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

您要与ranks列表进行比较,而不是实际(当前)rank值.

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

您的意思可能是:

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

修改

根据您的评论,我怀疑您想要的是这样的东西.

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

除非您希望能够更改任何已创建的卡的等级或花色,否则我建议将if语句从bjValue移到__init__.

这篇关于创建纸牌类Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆