;

Python Program to Shuffle a Deck of Cards


Tutorialsrack 23/04/2020 Python

In this Python Program, we will learn how to shuffle a deck of cards. In this program, we use the two modules for performing a shuffle of a deck of cards and these standard python modules are itertools and random

Here is the code of the program to shuffle a deck of cards.

Python Program to Shuffle a Deck of Cards
# Python Program to Shuffle Deck of Cards

# Import modules
import itertools, random

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

# shuffle the cards
random.shuffle(deck)

# draw five cards
print("You got:")
for i in range(5):
   print(deck[i][0], "of", deck[i][1])
Output

You got:

13 of Club

9 of Diamond

10 of Spade

8 of Club

10 of Heart

In this program, we use the product() function from the itertools module and this function performs the cartesian product of the two sequences, and the other function we used is shuffle() from the random module.


Related Posts



Comments

Recent Posts
Tags