Emma
Emma

Reputation: 35

Creating game of war (cards)-- having trouble getting cards to print

class PlayingCard:

    # constructor
    def __init__(self, rankNum, suitNum):
        self.rank = rankNum
        self.suit = suitNum

    def __str__(self):
        return "{} of {}".format(self.rank, self.suit)
    
    def getRank(self):
        if  self.rank == 11:
            return "Jacks"
        elif self.rank == 12:
            return "Queen"
        elif self.rank == 13:
            return  "King"
        elif self.rank == 14:
            return "Ace"
        else:    
            return self.rank
    
    def getSuit(self):
        if self.suit == 1:
            return "Clubs"
        elif self.suit == 2:
            return "Hearts"
        elif self.suit == 3:
            return "Diamonds"
        elif self.suit == 4:
            return "Spades"
                    
    def equals(self, otherCard):
        if self.rank == otherCard.rank and self.suit == otherCard.suit:
            return True
        else:
            return False

    def trumps(self, otherCard):
        if self.rank > otherCard.rank:
            return True
        else:
            return False

This is the code I have so far. It runs, but the output ends up being "(name) drew the 9 of 1" when I want it to print it as "9 of Hearts" (aka the card descriptor). Any clues on what I'm doing wrong?? How do I get getRank to return the string descriptions into self.rank? Also, I didn't include the rest of the code for the war game because it works completely, it's just something with the way I'm returning the ranks and suits. However, if it's helpful for me to add it, I will!

Upvotes: 0

Views: 26

Answers (1)

kn0wmad1c
kn0wmad1c

Reputation: 110

def __str__(self):
        return "{} of {}".format(self.rank, self.suit)

This is returning self.rank and self.suit.

So instead of Jack of Hearts, it would return 11 of 2.

To return Jack of Hearts, you need to change your code to this:

def __str__(self):
        return "{} of {}".format(self.getRank(), self.getSuit())

Upvotes: 1

Related Questions