【问题标题】:Python Card & Deck OOPPython Card & Deck OOP
【发布时间】:2021-02-07 14:59:42
【问题描述】:

我正在尝试编写一个具有以下功能的程序:

一个创建一副纸牌的函数,默认为 52 张,或在提供花色时仅为 13 张。

从卡组中删除第一张 card_count 卡的函数,将它们作为列表返回,并在卡用完时返回适当的错误。

预期结果如下。

>>> deck2 = Deck('♠')
>>> deck2.shuffle_deck()
>>> print(deck2.cards)
[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠, J of ♠, 9 of ♠, Q of ♠, 6 of ♠, 2 of ♠, K of ♠]

>>> deck2.deal_card(7)
[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠]

>>> deck2.deal_card(7)
Cannot deal 7 cards. The deck only has 6 cards left!

我的问题是每次运行代码时都会返回一个空列表。我想我已经正确设置了第一类(PlayingCard)和西装的可选参数。但是,我不太确定如何实现交易功能或为什么我的列表返回空。我错过了什么小东西吗?

import random

class PlayingCard():
    def __init__(self, rank, suit):
        acc_suit = ("♠", "♥", "♦", "♣")
        acc_rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
        assert rank in acc_rank, "Not a valid rank for a playing card."
        assert suit in acc_suit, "Not a valid suit for a playing card."
        self.suit = suit
        self.rank = rank
    
    def __repr__(self):
        return self.rank + ' of ' + self.suit

class Deck():
    
    def __init__(self, *suit):
        acc_suit = ("♠", "♥", "♦", "♣")
        self.cards = []
        if suit == None:
            for suit in range(4):
                for rank in range (1,14):
                    card = PlayingCard(rank, suit)
                    self.cards.append(card)
        if suit in acc_suit:
            for rank in range (1,14):
                card = PlayingCard(rank, suit)
                self.cards.append(card)
            

    def shuffle_deck(self):
        random.shuffle(self.cards)
    
    def deal_card(self):
        return self.cards.pop(0)
    
    
    def __str__(self):
        res = []
        for card in self.cards:
            res.append(str(card))
        return '\n'.join(res)

【问题讨论】:

    标签: python-3.x string class oop playing-cards


    【解决方案1】:

    您的列表为空的原因是,如果您提供一套西装,您就永远不会从一开始就创建套牌。调用

    deck2 = Deck('♠')
    

    将导致suit = ('♠',) - 1-tuple(包含 1 个字符串),因此它永远不会统计 if suit in acc_suit: => self.deck 为空。


    还有很多其他错误,以使您的代码可运行 - 在内联的 cmets 中指出:

    import random
    
    class PlayingCard():
        def __init__(self, rank, suit):
            # you try to create cards by range(2,14) but you assert that suit must
            # be 2 to 10 or "J"-"A" for picture cards
            # also: range(2,14) is [2..13] only so you miss a card
            r = {11:"J",12:"Q",13:"K",14:"A"}   
            rank = r.get(rank,rank) # translate 10 to 14 to "JQMA"
    
            acc_suit = ("♠", "♥", "♦", "♣")
            acc_rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
            assert rank in acc_rank, "Not a valid rank for a playing card."
            assert suit in acc_suit, "Not a valid suit for a playing card."
            self.suit = suit
            self.rank = rank
        
        def __repr__(self):
            # can not add a string to an integer (rank is an int for 2-10 !)
            return f"{self.rank} of {self.suit}"
    

    还有你的牌组:

    class Deck():
        
        # change this to a named param with default so you need to supply suit=' '       
        # omit the ,* if you do not like to force suit=" " when calling this
        def __init__(self, *, suit = None):
            acc_suit = ("♠", "♥", "♦", "♣")
            self.cards = []
            if not suit:
                for suit in range(4):
                    for rank in range (1,14):
                        card = PlayingCard(rank, suit)
                        self.cards.append(card)
            if suit in acc_suit:
                for rank in range (2,15):
                    card = PlayingCard(rank, suit)
                    self.cards.append(card)
    
        def shuffle_deck(self):
            random.shuffle(self.cards)
        
        # this function does not accept parameters like you posted its usage
        def deal_card(self, amount = 1):
            # always returns a list of cards, even if you only request one
            rv, self.cards = self.cards[:amount], self.cards[amount:]
            # this does NOT do the evaluation of enough cards are left, do it yourself:
            #  if len(rv) != amount: raise whatever
            return rv
    
        def __str__(self):
            return '\n'.join(str(c) for c in self.cards)
    

    测试:

    deck2 = Deck(suit = "♠")
    print(deck2.cards)
    deck2.shuffle_deck()
    print(deck2.cards) 
    print(deck2.deal_card(7))
    print(deck2.deal_card(7))
    

    输出:

    # orignal
    [2 of ♠, 3 of ♠, 4 of ♠, 5 of ♠, 6 of ♠, 7 of ♠, 8 of ♠, 9 of ♠, 10 of ♠, 
     J of ♠, Q of ♠, K of ♠, A of ♠]
    # shuffeled 
    [3 of ♠, 5 of ♠, 10 of ♠, 8 of ♠, A of ♠, 7 of ♠, 6 of ♠, 4 of ♠, J of ♠, 
     Q of ♠, 9 of ♠, 2 of ♠, K of ♠]
    # first 7
    [3 of ♠, 5 of ♠, 10 of ♠, 8 of ♠, A of ♠, 7 of ♠, 6 of ♠]
    # next 7 (only 6 remain)
    [4 of ♠, J of ♠, Q of ♠, 9 of ♠, 2 of ♠, K of ♠]
    

    【讨论】:

    • 回来了,因为我有足够的积分可以投票。谢谢!
    猜你喜欢
    • 2016-02-13
    • 2013-02-09
    • 2016-03-27
    • 1970-01-01
    • 2021-05-08
    • 2014-12-08
    • 2018-11-22
    • 2019-01-07
    • 2020-08-13
    相关资源
    最近更新 更多