简体   繁体   中英

Need help building a function in python to check a hand for pairs for the card game Go Fish

I am basically having a hard time figuring out how to set up a function to check a hand for pairs. I have the deck and cards set up to have the value and the suit so when I try to check the hand for pairs, even when there are pairs it does nothing. I know I need to slice the elements to get the value and then compare it to the other values in the hand, but I have been struggling with this for weeks and have made no progress whatsoever. This function is literally keeping me from turning in a few assignments so any help will be greatly appreciated.

    def check_hand_pairs(player_hand):
        number = 0
        size = len(player_hand)
        while number <= size:
            (This is where I get confused and have tried many things)

    player_1_hand = ['2H', '4D', '2D', 'JD', 'KS', 'QS', 'AS']
    # (Basically, I need to compare the first character in each element
    # in the hand and then remove the pairs.)


  

The simplest solution I could thinks is making a function that returns True / False if the player has a pair. You can check if a player has a pair using count method.

def has_pair(hand):
    """
    Returns True if the hand has a pair, False otherwise.
    """
    for card in hand:
        if hand.count(card) == 2:
            return True
    return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM