Python Program to Shuffle Deck of Cards

In this example, we will write a program that will shuffle deck of cards. To better understand this example, make sure you have knowledge of the following tutorials:-

Python Program to Shuffle Deck of Cards

import itertools, random

# make a deck of cards
cards = ['Spade', 'Heart', 'Diamond', 'Club']
deck = list(itertools.product(range(1, 14), cards))

# using random function to shuffle the deck
random.shuffle(deck)

# draw card from user
no_of_cards = int(input("How many cards you want to display?: "))
print("You got:")
for i in range(no_of_cards):
    print(deck[i][0], "of", deck[i][1])

The output of the above program is:-

How many cards you want to display?: 4
You got:
13 of Club
2 of Spade
7 of Diamond
11 of Spade

Program Explanation:-

The program generates random shuffle of cards from a deck of the card using python. The itertools module generates every possibility of the range of cards for Spade, Diamond, Heart, and Club. The random module will shuffle these decks. The input from the user is taken for how many cards a user wants to display and for loop is used to list the card.

SHARE Python Program to Shuffle Deck of Cards

You may also like...

Leave a Reply

Your email address will not be published.

Share