Python继承,将列表传递给另一个类

时间:2019-02-03 09:55:56

标签: python

我正在尝试使用Hand类中Deck类的卡片组列表,在“ deckCopy = deck”行中收到错误“ NameError:未定义名称'deck'”

class Deck(Card):
    def __init__(self):
        pass
    def get_cards(self):
        deck=[]
        for i in range(1,15):
            deck.append(Card("red","heart",i))
            deck.append(Card("red","diamond",i))
            deck.append(Card("black","club",i))
            deck.append(Card("black","spade",i))
        random.shuffle(deck)
        for i in deck:
            if i.number==11:
                deck.remove(i)
        return deck
class Hand(Deck):
    def __init__(self):
        pass
    deck=Deck.get_cards()
    deckCopy=deck
    random.shuffle(deckCopy)
    def get_hand():
        Hand=[]
        counter=1
        for i in deckCopy:
            Hand.append(i)
            deckCopy.remove(i)
            if counter==5:
                break
        return Hand

我希望返回4个Card对象的列表,不确定计数器是4还是5,但这并不重要。

2 个答案:

答案 0 :(得分:0)

这是我答案的修改版本,我之前误解了您的代码,这使我的答案显得无关紧要。

class Card:                                                                                                                                                                                   
    def __init__(self, color, suit, count):                                                                                                                                                   
        self.color = color                                                                                                                                                                    
        self.suit = suit                                                                                                                                                                      
        self.count = count  

class Deck(Card):                                                                                                                                                                             
    def __init__(self):                                                                                                                                                                       
        pass   

    @staticmethod                                                                                                                                                                             
    def get_cards():                                                                                                                                                                          
        deck=[]                                                                                                                                                                               
        for i in range(1,15):                                                                                                                                                                 
            deck.append(Card("red","heart",i))                                                                                                                                                
            deck.append(Card("red","diamond",i))                                                                                                                                              
            deck.append(Card("black","club",i))                                                                                                                                               
            deck.append(Card("black","spade",i))                                                                                                                                              
        random.shuffle(deck)                                                                                                                                                                  
         for i in deck:                                                                                                                                                                      
             if i.number==11:                                                                                                                                                                
                 deck.remove(i)                                                                                                                                                              
        return deck                                                                                                                                                                           

class Hand(Deck):                                                                                                                                                                             
    def __init__(self):                                                                                                                                                                       
        pass                                                                                                                                                                                  
    deck=Deck.get_cards()                                                                                                                                                                     
    deckCopy=deck                                                                                                                                                                             
    random.shuffle(deckCopy)                                                                                                                                                                  
    def get_hand():                                                                                                                                                                           
        Hand=[]                                                                                                                                                                               
        counter=1                                                                                                                                                                             
        for i in deckCopy:                                                                                                                                                                    
            Hand.append(i)                                                                                                                                                                    
            deckCopy.remove(i)                                                                                                                                                                
            if counter==5:                                                                                                                                                                    
                break                                                                                                                                                                         
        return Hand 

请注意,由于您未使用static,因此我将其中一种方法设为{{1}},因为该方法并未针对实例执行任何特定操作。每当您调用它时,它只会创建一副纸牌

答案 1 :(得分:0)

  

问题:我正在尝试使用Hand类中Deck类的卡片组列表

方法错误,Hand不应访问整个 Deck
要获得Hand,请问Deck 给我一个随机的Hand ,例如:

myDeck = Deck()
hand = myDeck.hand(5)
  

问题:我收到错误NameError: name 'deck' is not defined" in line "deckCopy=deck

这意味着您的Questions代码与您正在运行的代码不同。
您将获得:TypeError: get_cards() missing 1 required positional argument: 'self'
在第deck=Deck.get_cards()

您使用此符号 .get_cards() 呼叫Deck.get_cards(),它的名称为Deck.get_cards(self)。从Desk继承后,您可以简单地使用 deck=self.get_cards()


  

重新考虑您的整个方法,根本不需要inheritance

CardCardSuitSuit,而DeskDesk

  

注意:此示例未注意已使用的卡片!

import random

class Card:
    def __init__(self, color, symbol, rank):
        self.color = color
        self.symbol = symbol
        self.rank = rank

    def __str__(self):
        return '({} color:{}, rank:{})'.format(self.symbol, self.color, self.rank)

class Suit:
    def __init__(self, symbol):
        color = {'heart': 'red', 'diamond':'red', 'club':'black', 'spade':'black'}
        self.cards = []

        for rank in range(1, 11):
            self.cards.append(Card(color[symbol], symbol, rank))

        for rank in ['king', 'queen', 'jack', 'ace']:
            self.cards.append(Card(color[symbol], symbol, rank))

class Deck:
    def __init__(self):
        self.card_suits = []

        for symbol in ['heart', 'diamond', 'club', 'spade']:
            self.card_suits.append(Suit(symbol))

    def sample(self, count):
         cards = []
         for card_suit in self.card_suits:
            for card in card_suit.cards:
               cards.append(card)

         return random.sample(cards, count)

    def hand(self, count):
        return self.sample(count)
  

用法

if __name__ == '__main__':
    myDeck = Deck()

    for _ in range(3):
       hand = myDeck.hand(5)
       print('hand:{}'.format(', '.join([str(card) for card in hand])))
  

输出

hand:(heart color:red, rank:6), (club color:black, rank:king), (spade color:black, rank:9), (club color:black, rank:8), (diamond color:red, rank:queen)
hand:(diamond color:red, rank:8), (spade color:black, rank:5), (club color:black, rank:1), (club color:black, rank:5), (spade color:black, rank:2)
hand:(heart color:red, rank:10), (heart color:red, rank:6), (diamond color:red, rank:8), (club color:black, rank:3), (heart color:red, rank:ace)

使用Python测试:3.4.2