Python,纸牌游戏,AttributeError



我正在尝试使纸牌游戏成为"傻瓜"。但是当我运行程序时,我收到一个属性错误:

输入玩家数量: 1 玩家:玩家1包含:

手牌值:0 回溯(最近一次调用(:文件"main.py", 第 25 行,在 print(player( 文件 "C:\Users\kozus\Desktop\fool\fool\Hand.py",第 38 行,在 str 文本中 += "Hand value: " +str(self.getValue((( 文件 "C:\Users\kozus\Desktop\fool\fool\Hand.py",第 20 行,在 getValue 中 result += self.card.cardPoints(self( 文件 "C:\Users\kozus\Desktop\fool\fool\Card.py",第 8 行,在 cardPoints 中 如果 self.rank 在 ["10", "J", "Q", "K", "A"] 中: 属性错误: '手' 对象没有属性 'rank'

这是我的代码: 从随机导入随机播放

class Card(object):
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def getRank(self):
return self.rank
def cardPoints(self):
# Rerturns amount points for some card
if self.rank in ["10", "J", "Q", "K", "A"]:
if self.rank == "A":
# 11 points for ace
return 11
else:
# 10 points for 10, jack, queen или king
return 10
else:
# Amount points for any other card
return ["6", "7", "8", "9"].index(self.rank) + 6
def __str__(self):
return "%s %s" % (self.rank, self.suit)
class Hand(object):
def __init__(self, name):
self.name = name
self.cards = []
self.card = Card
def addCard(self, card):
# Add card in hand
self.cards.append(card)
def getValue(self):
# Amount points in hand
result = 0
# Amount aces in hand
aces = 0
for card in self.cards:
result += self.card.cardPoints(self)
# If ace in hand then we increasing amount of aces in hand
if card.getRank() == "A":
aces += 1
# Count aces like 1 or 11 points
if result + aces * 10 <= 21:
result += aces * 10
return result
def __str__(self):
text = "%s's contains:n" % self.name
for card in self.cards:
text += str(card) + " "
text += "nHand value: " + str(self.getValue())
return text
class Deck(object):
def __init__(self):
ranks = ["6", "7", "8", "9", "10", "J", "Q", "K", "A"] # Ranks
suits = ["D", "C", "H", "S"] # Suits
self.cards = [Card(r, s) for r in ranks for s in suits] # Generating deck, consists of 36 cards
shuffle(self.cards)
def dealCard(self):
# Card handing over function
self.cards.pop()
amount_players = int(input("Enter amount of players: "))
deck = Deck() # Creating deck
i = 0
# Creating players
players = [Hand("player{0}".format(i + 1)) for i in range(amount_players)]
print("Players: ")
for player in players:
# Printing players
print(player)
for player in players:
while i < 6:
# Distrbuting 6 card per player
player.addCard(deck.dealCard())
i += 1
for player in players:
print(player)

现在有人如何解决它吗?

我认为您不需要Hand中的card属性,因此您应该从Hand构造函数中删除初始化。 所有卡都已存储在cards中。

这将导致此错误:

File "/tmp/t.py", line 43, in getValue
result += self.card.cardPoints(self)
AttributeError: 'Hand' object has no attribute 'card'

在这里,问题是您不使用card循环迭代变量,因为您在对象中使用self查找它。cardPoints方法也不带任何参数(隐式self参数除外(,所以让我们也解决这个问题:

for card in self.cards:
result += card.cardPoints()

这将使您克服初始错误。

之后,您将不得不更改dealCard方法以实际返回从牌组中弹出的卡片,因为dealCard方法当前返回None,根本没有return语句。

# Change this line:
result += self.card.cardPoints(self)
# to this:
result += card.cardPoints()

您不需要将对象实例(自身(传递给'cardPoints。为什么你会有这个:

self.card = Card

没有必要这样做,你所做的只是使self.card类而不是对象。

最新更新