属性错误Blackjack Python

时间:2014-11-21 04:06:32

标签: python attributeerror blackjack

我必须从我的教科书中修改这个程序才能使用赌注,但我甚至无法运行原始程序。这是本书中出现的程序,除了每个类都在不同的页面上。我不确定是否需要输入所有这些课程,因为我的教授说采取二十一点程序并修改它以接受赌注,但它进口甲板和卡片所以我不确定。我把它们打出来以防万一,因为我觉得它不会造成很大的伤害,也许我错了。

class Card(object):

  RANKS=(1,2,3,4,5,6,7,8,9,10,11,12,13)
  SUITS=('Spades','Diamonds','Hearts','Clubs')

  def _init_(self,rank,suit):
    self.rank=rank
    self.suit=suit

  def _str_(self):
    if self.rank==1:
        rank='Ace'
    elif self.rank==11:
        rank='Jack'
    elif self.rank==12:
        rank='Queen'
    elif self.rank==13:
        rank='King'
    else:
        rank=self.rank
    return str(rank)+ ' of ' + self.suit

import random

class Deck(Card,object):

  def _init_(self):
    self._cards=[]
    for suit in Card.SUITS:
        for rank in Card.RANKS:
            c=Card(rank,suit)
            self._cards.append(c)

  def shuffle(self):
    random.shuffle(self._cards)

  def deal(self):
    if len(self)==0:
        return None
    else:
        return self._cards.pop(0)

  def _len_(self):
    return len(self._cards)

  def _str_(self):
    result=''
    for c in self._cards:
        result= result+str(c)+'\n'
    return result

class Player(Deck,object):

  def _init_(self, cards):
    self._cards= cards

  def _str_(self):
    result=",".join(map(str,self._cards))
    results += "\n " + str(self.getPoints()) + " points"
    return result

  def hit(self,card):
    self._cards.append(card)

  def getPoints(self):
    count=0
    for card in self._cards:
        if card.rank>9:
            count+=10
        elif card.rank==1:
            count+=11
        else:
            count+=card.rank
    for card in self._cards:
        if count <=21:
            break
        elif card.rank==1:
            count -= 10
    return count

  def hasBlackjack(self):
    return len(self._cards)==2 and self.getpoints()==21

class Dealer(Player,object):

  def _init_(self,cards):
    Player._init_(self,cards)
    self._showOneCard= True

  def _str_(self):
    if self._showOneCard:
        return str(self._cards[0])
    else:
        return Player._str_(self)

  def hit(self,deck):
    self._showOneCard= False
    while self.getPoints() <17:
        self.Cards.append(deck.deal())

class Blackjack(Player, object):

  def _init_(self):
    self._deck = Deck()
    self._deck.shuffle()

    self._player = Player([self._deck.deal(),
                           self._deck.deal()])
    self._dealer = Dealer([self._deck.deal(),
                           self._deck.deal()])

  def play(self):
    print ("Player:\n", self._player)
    print ("Dealer:\n", self._dealer)
    while True:
        choice = input("Do you want to hit? [y/n]: ")
        if choice in ("Y","y"):
            self._player.hit(self._deck.deal())
            points = self._player.getPoints()
            print ("Player:\n", self._player)
            if points >= 21:
                break
        else:
            break
    playerPoints = self._player.getPoints()
    if playerPoints> 21:
        print ("You bust and lose")
    else:
        self._dealer.hit(self._deck)
        print ("Dealer:\n", self._dealer)
        dealerPoints=self._dealer.getpoints()
        if dealerPoints >21:
            print ("Dealer busts you win")
        elif dealerPoints>playerPoints:
            print ("Dealer wins")
        elif dealerPoints<playerPoints and playerPoints <= 21:
            print ("You win")
        elif dealerPoints == playerPoints:
            if self._player.hasBlackjack() and not self._dealer.hasBlackjack():
                print ("You win")
        elif not self._player.hasBlackjack() and self._dealer.hasBlackjack():
            print ("Dealer wins")
        else:
            print ("There is a tie")

Blackjack().play()

我得到了

Traceback (most recent call last):
File "C:\Users\Schuler\bj.py", line 151, in <module>
Blackjack().play()
File "C:\Users\Schuler\bj.py", line 117, in play
print ("Player:\n", self._player)
AttributeError: 'Blackjack' object has no attribute '_player'

我直接从书中复制了所有内容,但我无法弄清楚出了什么问题。

_____的修改 _____

所以有人指出我忘记了双重下划线,所以我解决了这个问题。然后我跑了10次,发现它现在运行的小拼写错误。

1 个答案:

答案 0 :(得分:1)

看起来您在某些方法定义中遗漏了双下划线。它是__init__,而不是_init___str____len__和其他特殊类方法也是如此。

这些特殊的方法名称必须完全写入,否则它们不会起作用。例如,如果您有一个名为__init__的方法,那么当您实例化该类型的对象时,该方法中的代码将自动运行。因为你键入错了,所以方法永远不会运行,而你的代码也会出错。

__init__如何运作的简单说明:

class Player(object):
    def __init__(self):
        print 'new player object'

Player() # __init__() called automatically and prints 'new player object'