1

I am trying to use the card_number() and card_suit() methods from class Card to determine the last_card suit and number from class Rules

class Card:

    def __init__(self, card):
        self.card = card
        self.number = self.card_number(card)
        self.suit = self.card_suit(card)

    def card_number(self, card):
        card = self.card
        number = card[0]
        return number


    def card_suit(self, card):
        card = self.card
        suit = card[-1]
        return suit

class Rules(Card):

    def __init__(self, last_card, card):
        Card.__init__(self, card)
        self.last_card = last_card
        self.last_card_number = self.card_number(self.last_card)
        self.last_card_suit = self.card_suit(self.last_card)

It looks like that when I am accesing the function they save the card and suit only for the card variable, also in the last card

Now I'm getting:

a = Rules(last_card="As", card = "5c")
print(a.card) -> "5c"
print(a.number)-> "5"
print(a.suit) -> "c"

print("last_card")
print(a.last_card) -> "As"
print(a.last_card_number) -> "5"
print(a.last_card_suit) -> "c"

I'm expecting:

a = Rules(last_card="As", card = "5c")
print(a.card) -> "5c"
print(a.number)-> "5"
print(a.suit) -> "c"

print("last_card")
**print(a.last_card) -> "As"
print(a.last_card_number) -> "A"
print(a.last_card_suit) -> "s"**
khelwood
  • 55,782
  • 14
  • 81
  • 108
robertsci
  • 45
  • 1
  • 9
  • 1
    Not even sure why you need `Rules` to be a superclass of `Card` when you can just , when you need Rules to just hold two instances of `Card` – Devesh Kumar Singh Jun 26 '19 at 07:45

3 Answers3

1

As I mentioned in the comments, you do not need Rules to be a superclass of Card, when you need Rules to just hold two instances of Card

You can modify your class to achieve that as follows

class Card:

    #Instantiating instance attributes
    def __init__(self, card):
        self.card = card
        self.number = self.card[0]
        self.suit = self.card[-1]

class Rules:

    #Two instances of Card class
    def __init__(self, last_card, card):
        self.last_card = Card(last_card)
        self.card = Card(card)

a = Rules(last_card="As", card = "5c")
print(a.card.card)
print(a.card.number)
print(a.card.suit)

print(a.last_card.card)
print(a.last_card.number)
print(a.last_card.suit)

The output will be

5c
5
c
As
A
s

You can even get rid of Rules class and make two instances of Card classes, and use them instead

last_card = Card('As')
card = Card('5c')
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • Thanks for your answer. I thought about implementing the class Rules, because the rules of the are influenced about the last_card that is on the table. I'm trying to make a macao game. But i think your solution to have two instances of the card cold work, after wall the last card is still a card. – robertsci Jun 26 '19 at 08:06
0

The problem here is that you're overriding the parameter card of your methods in the Card class:

def card_number(self, card):
    card = self.card
    number = card[0]
    return number


def card_suit(self, card):
    card = self.card
    suit = card[-1]
    return suit

Remove the card = self.card lines here and it should work properly. It should look like this:

def card_number(self, card):
    number = card[0]
    return number


def card_suit(self, card):
    suit = card[-1]
    return suit
Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
Silveris
  • 1,048
  • 13
  • 31
  • 1
    Thank you for your answer, i tried to straight implement your recommendation, but it looks like i'm still missing something – robertsci Jun 26 '19 at 08:09
0

Update your code in this way,

class Card:

    def __init__(self, card):
        self.card = card
        self.number = self.card_number(card)
        self.suit = self.card_suit(card)

    @staticmethod
    def card_number(card):
        # card = self.card
        number = card[0]
        return number

    @staticmethod
    def card_suit(card):
        # card = self.card
        suit = card[-1]
        return suit


class Rules(Card):

    def __init__(self, last_card, card):
        Card.__init__(self, card)
        self.last_card = last_card
        self.last_card_number = self.card_number(self.last_card)
        self.last_card_suit = self.card_suit(self.last_card)
Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
  • Thanks for your answer, the output is the expected one. Could you point me to some learning resources from where can better understand how this decorators are working. I thought about using them, but i don't quite understand them yet – robertsci Jun 26 '19 at 08:08
  • You're welcome @robertsci, you can learn lot of thins from this [question](https://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod) and from this [article](https://www.pythoncentral.io/difference-between-staticmethod-and-classmethod-in-python/). Let me know if you get any error while running my code. – Kushan Gunasekera Jun 26 '19 at 08:18