Python program to generate a deck of cards

Mini Python Projects for Practice

Python program to generate a deck of cards

 

Project Description

One of Practity’s classic mini python projects for practice is the deck of cards generator. Your task is to writeย  a command line program that generates a standard 52-card French-suited deck. Moreover, the program must include a function to suffle so whenever you run it, the program will return 4 random cards.

The standard French deck includes the following cards:

  1. Four suits: clubs (โ™ฃ), diamonds (โ™ฆ), hearts (โ™ฅ) and spades (โ™ )
  2. Three court cards (face cards), King, Queen and Jack
  3. Each suit must also include ten numeral cards from one (Ace) to ten

Expected Output

# Print four random cards
print_random_cards(deck, 4)
9 of โ™  2 of โ™  King of โ™ฅ 6 of โ™ฃ

Steps

  1. Define the "suits" list and "ranks" list to represent the four suits and thirteen ranks of a standard deck.
  2. Create the "create_deck()" function that creates an empty "deck" list.
  3. Use nested loops to iterate over each suit and rank, and create a card string in the format of “{rank} of {suit}”. Append each card to the "deck" list.
  4. Return the "deck" list from the "create_deck()" function.
  5. Create the "shuffle_deck()" function that takes the "deck" list as an argument and shuffles it using the "random.shuffle()" function.
  6. Create the "print_random_cards()" function that takes the "deck" list and the desired number of cards as arguments.
  7. Use the "random.sample()" function to select num_cards number of random cards from the deck.
  8. Iterate over the "random_cards" list and print each card.
  9. Finally, create the "deck" list by calling the "create_deck()" function, shuffle it using the "shuffle_deck()" function, and print four random cards by calling the function with "deck" and “4" as arguments.

Mini Project Solutions

python mini project with solutions pdf

Check also Practity’s real Python projects.

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for practice
Register New Account
Shopping cart