为什么我收到“AttributeError”错误

时间:2017-10-29 23:52:30

标签: python

晚上好, 我有两个班,我正在和他一起工作 CardDeckService.py包含一个名为GetCard的方法,它返回一个字符串值:

import random
class CardDeckService:


      ShuffledCardDeck = None
      SelectedCards    = None
      MasterCardDeck   = None

      def __init__(self):
          self.ShuffledCardDeck = {}
          self.SelectedCards = {}
          self.MasterCardDeck = {}

      def BuildCardDeck(self):
          """
          BuildCardDeck() - builds a card deck of 52 cards 
          """
          cardDeck = {}
          cardTypes = "Clubs,Spades,Diamonds,Hearts".split(",")
          cardNumbers= "A,2,3,4,5,6,7,8,9,10,J,Q,K".split(",")
          try:
              # Loop through the cardTypes array
                for cardType in cardTypes:

                    # Loop through the cardNumbers array
                    for cardNumber in cardNumbers:

                        # Create the key
                        key = "{} - {}".format(cardNumber,cardType)

                        value = ""
                        # Determine value
                        if cardNumber == "A":
                            value = "1or10"
                        elif cardNumber == "Q" or cardNumber == "K" or cardNumber == "J":
                            value = "10"
                        else:
                            value = cardNumber

                        # Add to consumer MasterCardDeck dictionary
                        self.MasterCardDeck[key] = value
          except:
              print("BuildCardDeck() - Error")


      def ShuffleCardDeck(self):
          """
          ShuffleCardDeck() - Shuffles the card deck in a random order and returns the shuffle card deck back to the consumer.
          """
          try:
              self.ShuffledCardDeck = {}

              # Copy the keys (Cards Display Name) to a local list
              Cards = list(self.MasterCardDeck.keys())

              # Shuffle the keys using random.shuffle
              rnd = random.shuffle(Cards)

              # Set the shuffle keys into the new dictionary
              for Card in Cards:
                  self.ShuffledCardDeck[Card] = self.GetCardValue(Card)
          except:
              print("ShuffleCardDeck() - Error")

      def GetCardValue(self, CardKey):
          """
          GetCardValue(CardKey) - Retrieves a card's numeric value
          param CardKey: represents tehe the Card face name.
          """
          # Get the master card deck
          CardValue =self.MasterCardDeck[CardKey]

          # Return the card value back to the consumer
          return CardValue





      def **GetCard**(self):
          PlayingCard = "A - Clubs" # list(self.ShuffledCardDeck.keys())[0]

          # Remove the card from the ShuffledCardDeck
          #del self.ShuffledCardDeck[PlayingCard]

          # return the card back to the consumer
          return "A - Clubs"

Dealer.py包含一个名为DealStarterCards的方法,它将CardDeckService.GetCard方法分类以获取卡

import User
import CardDeckService
class Dealer:
      UserBetsAmount = 0
      PlayingCards = []
      UserCards = []      
      Players = []
      CardDeckSvc = None

      Players = []
      def __int__(self):
          self.CardDeckSvc = CardDeckService.CardDeckService()
          self.CardDeckSvc.BuildCardDeck()
          self.CardDeckSvc.ShuffleCardDeck()

      # Deals initlal cards to users
      def DealStarterCards(self):

          playingCards = []

          svc = self.CardDeckSvc
          playingCards = []
          playingCards.append(svc.GetCard())
          playingCards.append(svc.GetCard())

          return playingCards

出于某种原因,当我使用以下代码运行Dealer.py文件时:

dealer = Dealer()
print(dealer.DealStarterCards())

我收到以下错误: AttributeError:'NoneType'对象没有属性'GetCard'

我很困惑。请帮忙

提前感谢您

1 个答案:

答案 0 :(得分:5)

(评论太长了,所以我写了一个答案)。

您已将__init__拼错为__int__。不幸的是,这个故事并没有结束。

您最初还定义了一个类CardDeckSvc None。然后,您尝试在(拼写错误的)__init__构造函数中定义它。但是,解释器在创建对象时没有看到有效的构造函数,因此永远不会调用该方法,并且永远不会设置CardDeckSvc

因此,当您最终致电DealStarterCards时,self.CardDeckSvcNoneself.CardDeckSvc.GetCard()会引发AttributeError例外。