Home > front end >  How can I check the position of the first face card in a deck?
How can I check the position of the first face card in a deck?

Time:05-11

I have an assignment for school where I have to shuffle a deck of cards and pick out the first face card. I made if statements to check for the first position in the list but it's very clunky and I want to be able to check all positions without having to manually put it in. I am very new to Python and any help would really be appreciated.

import random

deck = ["AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", 
"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", 
"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", 
"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC"]

print(deck)

random.shuffle(deck)
print(deck)

if deck[0] == "JS":
  print(deck[0])
elif deck[0] == "QS":
  print(deck[0])
elif deck[0] == "KS":
  print(deck[0])
elif deck[0] == "JH":
  print(deck[0])
elif deck[0] == "QH":
  print(deck[0])
elif deck[0] == "KH":
  print(deck[0])
elif deck[0] == "JD":
  print(deck[0])
elif deck[0] == "QD":
  print(deck[0])
elif deck[0] == "KD":
  print(deck[0])
elif deck[0] == "JC":
  print(deck[0])
elif deck[0] == "QC":
  print(deck[0])
elif deck[0] == "KC":
  print(deck[0])

CodePudding user response:

You can loop over the list and check whether the card starts with 'JKQ'.

for i, card in enumerate(deck):
    if card.startswith('J') or card.startswith('Q') or card.startswith('K'):
        return {"index" : i, "card" : card }

CodePudding user response:

You have what would be described as a 'brute force' solution (i.e., manually checking against the set of all face cards).

You can improve it by changing your if statements. Instead of checking against every possible face card, instead just check if it is a J,Q,K.

I also don't see where you are iterating through the deck, you'll need a for loop for that.

CodePudding user response:

Could you do something like this?

import random

deck = ["AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", 
"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", 
"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD", 
"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC"]

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


def pull_first_face_card(deck):
    # Continue to pull cards until you get a face card
    while True:
        # Pull a card
        card = deck.pop(0)
        # If it's a face card, return it
        if card[0] in ["K", "Q", "J"]:
            return card


if __name__ == "__main__":
    # Shuffle the deck
    deck = shuffle(deck)
    # Pull the first face card
    card = pull_first_face_card(deck.copy())

    # Print the card
    print(card)
    # Print the deck
    print(deck)

CodePudding user response:

I would build the deck a bit differently using itertools.product then use a while loop to pull the first (and subsequent) face cards:

import itertools
import random
CARD_NUMBERS = ["A"]   [str(n) for n in range(2, 11)]
CARD_FACES = ["J", "Q", "K"]
CARD_SUITS = {"S", "H", "D", "C"}

deck = list(itertools.product(CARD_NUMBERS   CARD_FACES, CARD_SUITS))
random.shuffle(deck)
def next_face_card(deck):
    while deck:
        number, suit = deck.pop()
        if number in CARD_FACES:
            return number, suit

print(next_face_card(deck))
  • Related