我正在尝试编写二十一点程序,但是当我尝试将消息从我的牌组对象发送到另一个使用 .append() 的对象时,我不断收到 AttributeError: object has no 'append'
这是我认为相关的代码:
class Deck(Hand):
def deal(self, players, per_hand= 1):
for rounds in range(per_hand):
for hand in players:
if self.hand:
top_card = self.hand[0]
self.give(top_card,hand)
玩家参数是我使用实例化的对象元组:
class Bj_player(Player,Hand):
def __init__(self,name,score= 0,status= None):
self.name = name
self.score = score
self.status = status
self.hand = Hand()
Player 基类中除了用户的一些输入之外什么都没有。以上是我弄错的地方吗?
class Hand(object):
"""a hand of cards"""
def __init__(self):
self.hand = []
def add(self, card):
self.hand.append(card)
def give(self,card,other_hand):
if self.hand:
self.hand.remove(card)
other_hand.add(card)
else:
print("EMPTY ALREADY. COULD NOT GIVE")
我取出str部分将其剪掉。我得到的错误是:
line 44, in add
self.hand.append(card)
AttributeError: 'Hand' object has no attribute 'append'
我很新,所以解释得越简单越好。谢谢。
另外,以防万一它有帮助,这是我开始的方式:
deck = Deck() #populated and all that other stuff
total_players = Player.ask_number("How many players will there be? (1-6): ",1,6)
for player in range(total_players):
name = input("What is the player's name? ")
x = Bj_player(name)
roster.append(x)
deck.deal(roster,2) #error =(