python 纸牌_创建纸牌类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 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...

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值