【问题标题】:Appending from a deck of cards list to a players hand list in python从一副纸牌列表附加到python中的玩家手牌列表
【发布时间】:2022-06-17 19:54:19
【问题描述】:

我已经尝试了大约一个星期来完成这项工作。我的目标是获取卡片列表并将列表项 0、1 和 2 附加到 user_hand 列表中。当我运行代码时,我会得到诸如 ma​​in.Card object at 0x0000021DA02924D0> 打印而不是 A♥。我做错了什么来产生这样的错误?这是我第一次使用 python 类,所以我可能会在那里绊倒。

import random
user_hand = []
class Card():
    def __init__(self, name, suit):
        self.name = name
        self.suit = suit
    def print_card(self):
        suits = {"H":"♥","D":"♦","C":"♣","S":"♠"}
        print(f"{self.name}{suits[self.suit]}")

class Carddeck():
    def __init__(self):
        self.cards = []
        
        names = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")
        suits = ("H", "D", "C", "S")

        for suit in suits:
            for name in names:
                card = Card(name, suit)
                self.cards.append(card)

    def cupid(self):
        random.shuffle(self.cards)

    def dealing(self):
        user_hand.append(self.cards[0])
        user_hand.append(self.cards[1])
        user_hand.append(self.cards[2])

card = Card("A", "H")
card.print_card()
deck = Carddeck() # creates the deck.
deck.cupid() # shuffles the deck
deck.dealing() # deals to player, but this gives the strange output
for card in deck.cards:
    card.print_card()
print(user_hand)

【问题讨论】:

    标签: python class append playing-cards


    【解决方案1】:

    当 python 尝试打印列表时,它会打印每个对象的表示。您可以使用__repr__ 自定义此行为。你已经有一个print_card 方法,所以你可以修改它。然后,打印卡片,只需print(my_card)

    import random
    user_hand = []
    class Card():
        def __init__(self, name, suit):
            self.name = name
            self.suit = suit
    
        def __repr__(self):  # python uses this to print itself
            suits = {"H":"♥","D":"♦","C":"♣","S":"♠"}
            return f"{self.name}{suits[self.suit]}"
    
    class Carddeck():
        def __init__(self):
            self.cards = []
            
            names = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")
            suits = ("H", "D", "C", "S")
    
            for suit in suits:
                for name in names:
                    card = Card(name, suit)
                    self.cards.append(card)
    
        def cupid(self):
            random.shuffle(self.cards)
    
        def dealing(self):
            user_hand.append(self.cards[0])
            user_hand.append(self.cards[1])
            user_hand.append(self.cards[2])
    
    card = Card("A", "H")
    print(card)
    deck = Carddeck()
    deck.cupid()
    deck.dealing()
    for card in deck.cards:
        print(card)
    print(user_hand)
    # A♥
    # 8♠
    # 10♦
    # 2♠
    # 2♦
    # ...
    # [8♠, 10♦, 2♠]
    

    【讨论】:

      【解决方案2】:

      我会创建一个名为 Player 的新类,并将 self.hand 作为一个空列表,然后您可以用抽出的牌填充手牌

      您还需要在 Card 类中将 show 定义为您的 print(f"{self.name}{self.suit}")

      除了你使用的交易,我使用

      def drawCard(self): 
           card = self.cards.pop() 
           return card
      
      #Create Player      
      class Player(object):
          def __init__(self, name):
              self.name = name
              self.hand = []
          
          def draw(self, deck):
              self.hand.append(deck.drawCard())
      
          def showHand(self):
              for card in self.hand:
                  card.show()
                  
          def showcard(self):     
              for card in self.hand:
                  card.render()
          
          def discard(self):
              return self.hand.pop()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-07
        • 2017-10-06
        • 1970-01-01
        • 2021-08-18
        相关资源
        最近更新 更多