【问题标题】:Nested Dealer::Card::Deck class Deck.cards gets the list during the build method, but list is gone during shuffle method嵌套 Dealer::Card::Deck 类 Deck.cards 在 build 方法期间获取列表,但在 shuffle 方法期间列表消失
【发布时间】:2021-09-22 23:22:28
【问题描述】:

我已经稍微简化了代码,因为它是更大的烧瓶项目的一部分。但是问题仍然存在:

import random
class Dealer:
    def __init__(self):
        self.deck = self.Deck()
    class Deck:
        def __init__(self):
            self.cards = []
            self.cards = self.build()
        def build(self):
            for suit in ['Spades', 'Diamonds', 'Hearts', 'Clubs']:
                if suit == 'Spades':
                    suit_url='S.png'
                elif suit == 'Diamonds':
                    suit_url="D.png"
                elif suit == "Hearts":
                    suit_url="H.png"
                elif suit == "Clubs":
                    suit_url="C.png"
                for val in [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]:
                    img_url = (str(val) + suit_url)
                    self.cards.append(self.Card(val, suit, img_url))
        def shuffle(self):
            for i in range(len(self.cards)-1, 0, -1):
                rand_num = random.randint(0, i)
                self.cards[i], self.cards[rand_num] = self.cards[rand_num], self.cards[i]
        class Card:
            def __init__(self, value, suit, img):
                self.value = value
                self.suit = suit
                self.img = img                
dealer = Dealer()
deck = dealer.Deck()
deck.shuffle()

cards list 在 Deck 构建方法中显示了一个有效的卡片对象列表,但是当它到达 shuffle 方法时,cards 在调试器中显示没有?

【问题讨论】:

  • 我尝试将卡片组和嵌套卡片类移到 Dealer 类之外,它们通过卡片组,但 '''deck.cards''' 仍然没有返回。不确定我做错了什么,卡片道具在甲板内是可迭代的,但不是在构建方法完成后。

标签: class nested attributes python-3.8 nonetype


【解决方案1】:

怎么了:

'self.build()' 方法不返回任何东西(VOID)它只是更新'self.cards'。但是'self.cards' 与'self.build()' 的输出相当。 但是输出是 none,当你想使用 'deck.shuffle()' 时,你试图得到 none 的长度。

如何解决:

只需调用 build 方法来填卡。

import random
class Dealer:
    def __init__(self):
        self.deck = self.Deck()
    class Deck:
        def __init__(self):
            self.cards = []
            # Just call build method to fill the cards
            self.build()
        def build(self):
            for suit in ['Spades', 'Diamonds', 'Hearts', 'Clubs']:
                if suit == 'Spades':
                    suit_url='S.png'
                elif suit == 'Diamonds':
                    suit_url="D.png"
                elif suit == "Hearts":
                    suit_url="H.png"
                elif suit == "Clubs":
                    suit_url="C.png"
                for val in [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]:
                    img_url = (str(val) + suit_url)
                    self.cards.append(self.Card(val, suit, img_url))
        def shuffle(self):
            for i in range(len(self.cards)-1, 0, -1):
                rand_num = random.randint(0, i)
                self.cards[i], self.cards[rand_num] = self.cards[rand_num], self.cards[i]
        class Card:
            def __init__(self, value, suit, img):
                self.value = value
                self.suit = suit
                self.img = img                
dealer = Dealer()
deck = dealer.Deck()
deck.shuffle()

【讨论】:

  • 谢谢,是的,我今天早上自己发现了这个,因为我正在更改无效的可笑随机播放方法!
  • 仅供参考@Milad,我会投赞成票,但我对stackoverflow 太陌生,还没有那种能力!但我真的很感谢你的回答。非常感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
相关资源
最近更新 更多