User Tools

Site Tools


clubs:python_club:python_club_ex_shuffle_deck_of_cards
Home | clubs :: cloud club :: python_club :: 3D-Printing | projects :: Proxmox | Kubernetes | scripting | utilities | games

About the Club

Python Club Topics - Exercise: Shuffle a deck of cards

Exercise: Shuffle and deal cards from a deck of playing cards

  1. Output: Display random cards from a virtual deck
  2. What you learn from the example:
    1. Importing and using library modules
    2. Using loops and lists

Solution

[1] code:python show
# Python program to shuffle a deck of card

# importing 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])
clubs/python_club/python_club_ex_shuffle_deck_of_cards.txt · Last modified: by 127.0.0.1