0

I am currently working on an assignment where in a particular question I have to take a list of playing cards and, using a class, figure out if it is a Royal Flush.

The lecturer provided a 'skeleton' of code that I have to build the rest around without changing the parts he wrote.

#Lecturer created...
class PokerHand(Hand):

    def __init__(self, cards = list()):
        Hand.__init__(self, cards)

        self.handRank = 0
        self.hand = "High Card"

        #I have so far added this part...
        total_value = 0
        val_card_b4 = 0
        for card in self.cards:
            if Card.getValue(card) > val_card_b4:
                total_value += Card.getValue(card)
                val_card_b4 = Card.getValue(card)
                checkRoyalFlush()
        #...to here. However it throws an error that checkRoyalFlush isn't defined.        

    #The lecturer then had what is below already added.
    def checkHand(self):

        if self.checkRoyalFlush():
            self.handRank = 9
            self.hand = "Royal Flush"
            print("Test")

I have already created a Card class in an earlier question that allows me to create a card object get the value of the card (A=11, 2-10 equal face value etc.)

My problem is that, once I have checked the cards, I don't know how to 'activate' the if self.checkRoyalFlush(): statement in the checkHand Method.

The code I have to get running is:

h1 = PokerHand([Card('hearts', '10'), Card('clubs', '10'),Card('hearts', '2'),Card('hearts', '3'),Card('spades', 'J')])
h1.show()
print(h1.checkHand())

I would like to understand how to get the if statement working, as I have spent a lond time researching and can't figure it out. I am only a beginner in python and new to the Object Oriented side of it.

Edit: I also don't know how to define 'checkRoyalFlush' without it getting more errors

4
  • I assume you need to write self.checkRoyalFlush() yourself. It is called by calling checkHand. Commented Mar 13, 2017 at 13:34
  • @Morgoth, yes, I should edit my Q to say that actually thnks. I don't actually know how to define it, or rather what to let it equal to initially Commented Mar 13, 2017 at 13:36
  • 1
    @Gurfuffle It appears to want to be a method. Define it similarly to your def checkHand(self): method Commented Mar 13, 2017 at 13:41
  • The call you have to checkRoyalFlush() should also be self.checkRoyalFlush() unless it is a global function. Hence the undefined error. Commented Mar 13, 2017 at 13:42

2 Answers 2

1

An if statement such as if self.checkRoyalFlush(): requires a boolean data type as a result, i.e. True or False. Your method needs to return either one of those values:

#Lecturer created...
class PokerHand(Hand):

    def __init__(self, cards = list()):
        # etc...

    def checkHand(self):
        # etc...

    # add your new method below the methods that already exist
    def checkRoyalFlush(self):
        # paste your code to check if it is a royal flush here
        # if it is a royal flush, then:
            return True
        # if it is NOT a royal flush, then:
            return False

Also you need to refer to your method as self.checkRoyalFlush() as it is a part of the class PokerHand. You aren't doing that in checkHand() method.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like your lecturer want method called checkRoyalFlush() which I'm assuming will return true if your hand is a royal flush or false if it isn't aren't.

Also note that I don't know how you set up your card class, and I don't know what you are calling the suit or value attribute. In the code below, I call .suit for the suit attribute and .value and the value attribute. Change it to whatever you made it as.

Consider code below:

class PokerHand:
    def __init__(self, cards = list()):
        #your init goes here as above

    def checkHand(self):
        #checkHand as above

    def checkRoyalFlush(self):          
        check_suit = cards[0].suit   #note I don't know what you are calling the suits and values in your hand, 
        values = ['A','K','Q','J','10'] #values we want to check against
        for each_card in cards:
            if not (each_card.typing == check_suit and each_card.value in values): 
                return False
            values.remove(each_card.value) #once we found a value we want to remove it from the possible list
        return True

The method checkRoyalFlush() will take one card's suit out of the cards list. Since a royal flush must have all the same suit it doesn't matter which card I choose. Here I choose the first card in the list.

Then I iterate through the cards list and check if NOT each of the card's typing is the same, and if each of the values are in the values list

if one card is does not match the requirement, it returns False.

We remove the value we checked so we can make sure it's 1 value and not duplicated values. If the for loop checking is finished with out returning False, we know that it's a royal flush

Note this is not the most optimal way to do it, it's just one way that shows how it can be done rather clearly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.