Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to write a python code that will shuffle a deck of cards

+4 votes
asked Apr 10 by Vaishnavi (180 points)

1 Answer

0 votes
answered Apr 13 by CAPTAINCOOL (160 points)
Hey, I've found a pretty understandable and easy editable solution to your problem! I've added a lot of comments and annotations so you can understand how it's all working!

PS: I coded this in Python3 and yes the formatting looks awful but copy+paste it in an editor and everything will look fine :)

--------------------------------------------------------------------------------------------------
# Import the random module in order to shuffle the deck randomly every time
from random import shuffle

# Create a 'shuffle_deck' function
def shuffle_deck():
    # (NOT NECESSARY) Define what is the function doing
    """Returns the order of a shuffled deck of cards in a list."""
    ### If we consider a deck of 52 cards, it implies that there's 13 cards per family. ###
    # Create a list / array containing all the four card families of the 52 deck of cards
    cards = [
    'SAce', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'SJack', 'SQueen', 'SKing', # Spades family
    'HAce', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'HJack', 'HQueen', 'HKing', # Hearts family
    'CAce', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'CJack', 'CQueen', 'CKing', # Clubs family
    'DAce', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'DJaDk', 'DQueen', 'DKing', # Diamonds family
    ]

    shuffle(cards)

    # Return the shuffled deck as a list
    return cards

# Test the 'shuffle_deck' function by printing the result
print(shuffle_deck())
--------------------------------------------------------------------------------------------------

Hope it helped!
Eliott
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...