How to perform multiple 5‑card draws from a shuffled 52‑card deck in Python

1 Answer

0 votes
import random

"""
A standard deck of 52 playing cards consists of four suits:
spades (♠), hearts (♥), diamonds (♦), and clubs (♣).

Each suit contains 13 ranks:
Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King.

Cards of spades and clubs are black, while hearts and diamonds are red.

Color is informational only — it does not affect shuffling or random selection.
"""

"""
    This program performs multiple draws of 5 random cards from a 52‑card deck
    WITHOUT duplicates across draws.

    It uses:
    - Python lists for the deck
    - random.shuffle for efficient randomization (Fisher–Yates internally)
    - Functions for clean structure

    Algorithm:
    1. Build a full deck (52 cards)
    2. For each draw:
       a. Shuffle the remaining deck
       b. Draw 5 cards
       c. Remove them from the deck
"""

def build_deck():
    ranks = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
    suits = ["Clubs", "Diamonds", "Hearts", "Spades"]

    deck = [f"{rank} of {suit}" for suit in suits for rank in ranks]
    return deck

def draw_five(deck, draw_number):
    # Shuffle the remaining deck before this draw
    random.shuffle(deck)

    print(f"Draw #{draw_number}:")
    for card in deck[:5]:
        print(" ", card)
    print()

    # Remove drawn cards
    del deck[:5]

def main():
    # Number of draws
    draws = 3

    deck = build_deck()

    # Perform multiple draws (no duplicates)
    for i in range(1, draws + 1):
        if len(deck) < 5:
            print(f"Not enough cards left for draw #{i}.")
            break

        draw_five(deck, i)

main()


"""
run:

Draw #1:
  J of Spades
  6 of Hearts
  7 of Spades
  10 of Clubs
  3 of Clubs

Draw #2:
  A of Diamonds
  8 of Clubs
  4 of Diamonds
  8 of Hearts
  2 of Diamonds

Draw #3:
  4 of Spades
  9 of Diamonds
  J of Hearts
  7 of Hearts
  Q of Clubs

"""

 



answered 10 hours ago by avibootz

Related questions

...