Making the Solitaire game in Python #1 — Drawing cards from a shuffled deck
In this series I will show you step-by-step how I made the card game Solitaire in Python. A great project to practice programming skills.

In the first part of this series, we will start with creating a deck of 52 playing cards. The deck should be able to be shuffled and we must be able to draw cards from them.
Creating a deck of 52 cards
We will write a function that loops over the four suits: Clubs, Diamonds, Spades and Hearts. For each suit we loop over the cards from Ace to King. And we append a tuple containing a combination of suit and card to an empty list called deck. At the end of the function we return the list deck, which by then is filled with 52 unique cards.
def create_deck():
suits = ['Clubs','Diamonds','Spades','Hearts']
cards = ['Ace','2', '3', '4', '5', '6', '7', '8', '9', '10',
'Jack', 'Queen', 'King']
deck = []
for suit in suits:
for card in cards:
deck.append((suit,card))
return deckHere is how to test the create_deck function:
deck = create_deck()
print(deck)Here is the result:
[(‘Clubs’, ‘Ace’), (‘Clubs’, ‘2’), (‘Clubs’, ‘3’), (‘Clubs’, ‘4’), (‘Clubs’, ‘5’), (‘Clubs’, ‘6’), (‘Clubs’, ‘7’), (‘Clubs’, ‘8’), (‘Clubs’, ‘9’), (‘Clubs’, ‘10’), (‘Clubs’, ‘Jack’), (‘Clubs’, ‘Queen’), (‘Clubs’, ‘King’), (‘Diamonds’, ‘Ace’), (‘Diamonds’, ‘2’), (‘Diamonds’, ‘3’), (‘Diamonds’, ‘4’), (‘Diamonds’, ‘5’), (‘Diamonds’, ‘6’), (‘Diamonds’, ‘7’), (‘Diamonds’, ‘8’), (‘Diamonds’, ‘9’), (‘Diamonds’, ‘10’), (‘Diamonds’, ‘Jack’), (‘Diamonds’, ‘Queen’), (‘Diamonds’, ‘King’), (‘Spades’, ‘Ace’), (‘Spades’, ‘2’), (‘Spades’, ‘3’), (‘Spades’, ‘4’), (‘Spades’, ‘5’), (‘Spades’, ‘6’), (‘Spades’, ‘7’), (‘Spades’, ‘8’), (‘Spades’, ‘9’), (‘Spades’, ‘10’), (‘Spades’, ‘Jack’), (‘Spades’, ‘Queen’), (‘Spades’, ‘King’), (‘Hearts’, ‘Ace’), (‘Hearts’, ‘2’), (‘Hearts’, ‘3’), (‘Hearts’, ‘4’), (‘Hearts’, ‘5’), (‘Hearts’, ‘6’), (‘Hearts’, ‘7’), (‘Hearts’, ‘8’), (‘Hearts’, ‘9’), (‘Hearts’, ‘10’), (‘Hearts’, ‘Jack’), (‘Hearts’, ‘Queen’), (‘Hearts’, ‘King’)]
As you can see, all the cards are there, but they are all in order. So now we will look into a way to shuffle the deck for us.
Shuffling decks
When you shuffle a deck you basically put the cards in a random order. In our case we have to put the card-tuples in our list deck in a random order. This can be easily done with the shuffle function from the standard package: random. All we have to do is pass the deck list in the function call.
from random import shuffle
def create_deck():
suits = ['Clubs','Diamonds','Spades','Hearts']
cards = ['Ace','2', '3', '4', '5', '6', '7', '8', '9', '10',
'Jack', 'Queen', 'King']
deck = []
for suit in suits:
for card in cards:
deck.append((suit,card))
return deck
deck = create_deck()
shuffle(deck)
print(deck)The shuffle function does not return a new list but just rearranges the existing one. Here is the result:
[(‘Clubs’, ‘5’), (‘Diamonds’, ‘6’), (‘Diamonds’, ‘King’), (‘Spades’, ‘Jack’), (‘Spades’, ‘Ace’), (‘Diamonds’, ‘Jack’), (‘Clubs’, ‘3’), (‘Diamonds’, ‘Ace’), (‘Spades’, ‘2’), (‘Hearts’, ‘9’), (‘Hearts’, ‘4’), (‘Hearts’, ‘10’), (‘Clubs’, ‘9’), (‘Hearts’, ‘Queen’), (‘Spades’, ‘5’), (‘Clubs’, ‘Jack’), (‘Hearts’, ‘8’), (‘Spades’, ‘8’), (‘Spades’, ‘King’), (‘Diamonds’, ‘8’), (‘Spades’, ‘Queen’), (‘Spades’, ‘4’), (‘Clubs’, ‘8’), (‘Clubs’, ‘Queen’), (‘Clubs’, ‘2’), (‘Hearts’, ‘7’), (‘Hearts’, ‘5’), (‘Clubs’, ‘Ace’), (‘Hearts’, ‘6’), (‘Diamonds’, ‘3’), (‘Diamonds’, ‘4’), (‘Clubs’, ‘6’), (‘Hearts’, ‘King’), (‘Diamonds’, ‘7’), (‘Diamonds’, ‘9’), (‘Clubs’, ‘10’), (‘Hearts’, ‘Ace’), (‘Spades’, ‘7’), (‘Diamonds’, ‘10’), (‘Spades’, ‘3’), (‘Clubs’, ‘King’), (‘Spades’, ‘6’), (‘Hearts’, ‘2’), (‘Diamonds’, ‘Queen’), (‘Spades’, ‘10’), (‘Diamonds’, ‘2’), (‘Hearts’, ‘Jack’), (‘Clubs’, ‘4’), (‘Clubs’, ‘7’), (‘Hearts’, ‘3’), (‘Diamonds’, ‘5’), (‘Spades’, ‘9’)]
Drawing cards from a deck
Now we have a deck that is shuffled, it would be nice if we could draw cards from it. We can easily imitate the drawing of a card by using the pop method. This method removes the last item from a list and returns it. Meaning that not only we get a random card, but that card is also removed from the deck.
Here is a test of using the pop method to draw a card:
deck = create_deck()
shuffle(deck)
print(len(deck))
drawn_card = deck.pop()
print(drawn_card)
print(len(deck))With this as the result:
52
('Clubs', '7')
51The drawn card is 7 of Clubs. As you can see after drawing a card, the deck’s length is reduced from 52 to 51.
Keep in mind that everytime you run the code the deck gets put in a random order, so another card will highly likely be drawn. Here is a second test result:
52
('Diamonds', 'Ace')
51That concludes it for this part of the creating Solitaire in Python series. Please consider following me to stay up-to-date on when the next part is released.
Thank you for reading!
You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:






