为什么我会得到“属性错误”

2024-09-29 02:19:39 发布

您现在位置:Python中文网/ 问答频道 /正文

大家晚上好, 我现在有两个班 CardDeck服务.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"

你知道吗经销商.py包含名为DealStarterCards的方法CardDeck服务.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

因为某种原因当我运行经销商.py文件使用以下代码:

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

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

我很困惑。请帮忙

先谢谢你


Tags: theto方法selfvaluedefrandomkeys
1条回答
网友
1楼 · 发布于 2024-09-29 02:19:39

(这篇评论太长了,所以我正在写一个答案)。你知道吗

您将__init__拼错为__int__。不幸的是,故事并没有就此结束。你知道吗

您还定义了一个类变量CardDeckSvc,最初是None。然后尝试在(拼写错误的)__init__构造函数中定义它。但是,解释器在创建对象时看不到有效的构造函数,因此从不调用方法,也从不设置CardDeckSvc。你知道吗

因此,当您最终调用DealStarterCards时,self.CardDeckSvcNone,并且self.CardDeckSvc.GetCard()抛出一个AttributeError异常。你知道吗

相关问题 更多 >