我正试图使纸牌游戏变得“傻瓜”。但是当我运行programm时,出现了AttributeError:
输入玩家数量:1 玩家:player1包含:
手值:0追溯(最近一次呼叫最近):文件“ main.py”, 第25行,在 打印(播放器)文件“ C:\ Users \ kozus \ Desktop \ fool \ fool \ Hand.py”,第38行, str 文字+ =“ \ nHand value:” + str(self.getValue())文件“ C:\ Users \ kozus \ Desktop \ fool \ fool \ Hand.py”,第20行,位于getValue中 结果+ = self.card.cardPoints(self)文件“ C:\ Users \ kozus \ Desktop \ fool \ fool \ Card.py”,第8行,在cardPoints中 如果self.rank在[“ 10”,“ J”,“ Q”,“ K”,“ A”]中:AttributeError:'Hand'对象没有属性'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)
现在有人解决了吗?
答案 0 :(得分:1)
我认为您不需要card
中的Hand
属性,因此您应该从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
声明。
答案 1 :(得分:0)
# Change this line:
result += self.card.cardPoints(self)
# to this:
result += card.cardPoints()
您不需要将对象实例(自身)传递给`cardPoints。以及为什么要这样:
self.card = Card
没有必要,您要做的只是使self.card
成为类而不是对象。