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:
- Four suits: clubs (โฃ), diamonds (โฆ), hearts (โฅ) and spades (โ )
- Three court cards (face cards), King, Queen and Jack
- 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
- Define the
"suits"list and"ranks"list to represent the four suits and thirteen ranks of a standard deck. - Create the
"create_deck()"function that creates an empty"deck"list. - 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. - Return the
"deck"list from the"create_deck()"function. - Create the
"shuffle_deck()"function that takes the"deck"list as an argument and shuffles it using the"random.shuffle()"function. - Create the
"print_random_cards()"function that takes the"deck"list and the desired number of cards as arguments. - Use the
"random.sample()"function to selectnum_cardsnumber of random cards from thedeck. - Iterate over the
"random_cards"list and print each card. - 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
Check also Practity’s real Python projects.