Search code examples
pythonpython-3.xinheritancemultiple-inheritance

Multiple Inheritance Issue


I have encountered an issue which I cannot seem to overcome.

I am creating classes which are responsible for part of the functionality to make a Blackjack game. The issue I am encountering is that I cannot seem to access'single_card' variable in class 'GetValue'. single_card is returned from a method in the Deck class but I cannot seem to access it in the GetValue class.

I have been researching multiple inheritance with no luck, can anyone help me spot where I might be going wrong and help educate me on how to fix the issue.

Current Error: line 40, in init__Card.__init(self) TypeError: init() missing 2 required positional arguments: 'suit' and 'rank'

Many thanks in advance!

P.S - I am new to user on stackoverflow so apologies if my question asking skills are not that great!

Code below: '''

values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10,
          'Jack': 10,'Queen': 10, 'King': 10, 'Ace': 11}

class Deck():

    def __init__(self):
        suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
        ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
        self.deck = []  # start with an empty list
        for suit in suits:
            for rank in ranks:
                self.deck.append(Card(suit, rank))

    def shuffle(self):
        random.shuffle(self.deck)

    def __str__(self):
        deck_comp = ""
        for card in self.deck:
            deck_comp += '\n'+ card.__str__()
        return "The deck has: "+deck_comp

    def deal(self):
        self.single_card = self.deck.pop()
        return self.single_card

class Card():

    def __init__(self, suit, rank):
        self.suit = suit
        self.rank = rank

    def __str__(self):
        return self.rank+" of "+self.suit

class GetValue(Deck, Card):

    def __init__(self):
        Deck.__init__(self)
        Card.__init__(self)
        self.cardtype = Deck.single_card.split()
        self.cardval = values.get(self.cardtype[0])
        print(self.cardval)

mydeck = Deck()
print(mydeck.deal())
myval = GetValue()

'''


Solution

  • It seems that value relates to the numeric value corresponding to a card's rank, so it's an attribute of a card. So you could give Card a get_value method that returns the value:

    class Card:
        def __init__(self, suit, rank):
            self.suit = suit
            self.rank = rank
    
        def __str__(self):
            return self.rank + " of " + self.suit
    
        def get_value(self):
            return values[self.rank]
    
    mydeck = Deck()
    card = mydeck.deal()
    print(card)
    print(card.get_value())
    

    Another approach might be to pass value to Card.__init__, so it can be accessed as a simple attribute.

    class Card:
        def __init__(self, suit, rank, value):
            self.suit = suit
            self.rank = rank
            self.value = value
    
    card = Card('Hearts', 'three', 3)
    print(card.value)