Python Program to Pick a Random Card

In the previous article, we have discussed Python Program to Print Non Square Numbers
Random Module in python :

As this Random module is one of Python’s predefined modules, its methods return random values.

It selects integers uniformly from a range. For sequences, it has a function to generate a random permutation of a list in-place, as well as a function to generate a random sampling without replacement. Let’s take a look at how to import the Random Module.

The random module in Python is made up of various built-in Methods.

To pick a random card from a deck of cards in Python, you must first store all of the cards. Then select a card at random. However, there are 52 cards. I don’t think it’s a good idea to keep all of the cards in a list one by one.

So we’ll figure out a better way to do this.

  • First, make a list of all the card values (values range from 2 to A).
  • Next, make a list of the card signs. (Heart, Club, Spades, Diamond).

Examples:

Example1:

Input:

Given card _points = ['A', 'K', 'Q', 'J', '2', '3', '4', '5', '6', '7', '8', '9', '10']
Given card_signs = ['Heart','CLUB','DIAMOND','SPADE']

Output:

The random card from a deck of cards =  ('9', 'CLUB')

Example2:

Input:

Given card _points = ['A', 'K', 'Q', 'J', '2', '3', '4', '5', '6', '7', '8', '9', '10']
Given card_signs = ['heart ','club', 'spade', 'diamond']

Output:

The random card from a deck of cards =  ('J', 'Heart')

Program to Pick a random card

Below are the ways to pick a random card from the deck of cards.

Method #1: Using random.choice Method (Static input)

Approach:

  • Import random module using the import keyword.
  • Give the first list of card values as static input and store it in a variable.
  • Give the second list of card signs as static input and store it in another variable.
  • Apply random. choice() method for the first list to get the random item and store it in another variable.
  • Apply random. choice() method for the second list to get the random item and store it in another variable.
  • Get the random card by assigning both obtained random cards and store it in another variable.
  • Print the random card from the deck of cards.
  • The Exit of the program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the first list of card values as static input say and store it in a variable.
card_points = ['A', 'K', 'Q', 'J', '2',
               '3', '4', '5', '6', '7', '8', '9', '10']
# Give the second list of card signs as static input and store it in another variable.
card_signs = ['Heart', 'CLUB', 'DIAMOND', 'SPADE']
# Apply random. choice() method for the first list to get the random item and
# store it in another variable.
randm_card = random.choice(card_points)
# Apply random. choice() method for the second list to get the random item and
# store it in another variable.
randm_sign = random.choice(card_signs)
# Get the random card by assigning both obtained random cards and store it in
# another variable.
rndm_crd = randm_card, randm_sign
# Print the random card from the deck of cards.
print("The random card from a deck of cards = ", rndm_crd)

Output:

The random card from a deck of cards =  ('9', 'CLUB')

Method #2: Using random.choice Method (User input)

Approach:

  • Import random module using the import keyword.
  • Give the first list of card values as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Give the second list of card signs as user input using list(),map(),input(),and split() functions and store it in another variable.
  • Apply random. choice() method for the first list to get the random item and store it in another variable.
  • Apply random. choice() method for the second list to get the random item and store it in another variable.
  • Get the random card by assigning both obtained random cards and store it in another variable.
  • Print the random card from the deck of cards.
  • The Exit of the program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the first list of card values as user input using list(),map(),input(),and split() functions 
#and store it in a variable.
card_points = list(map(str, input( 'Enter some random List Elements separated by spaces = ').split()))
# Give the second list of card signs as user input using list(),map(),input(),and split() functions
#and store it in another variable.
card_signs = list(map(str, input( 'Enter some random List Elements separated by spaces = ').split()))
# Apply random. choice() method for the first list to get the random item and
# store it in another variable.
randm_card = random.choice(card_points)
# Apply random. choice() method for the second list to get the random item and
# store it in another variable.
randm_sign = random.choice(card_signs)
# Get the random card by assigning both obtained random cards and store it in
# another variable.
rndm_crd = randm_card, randm_sign
# Print the random card from the deck of cards.
print("The random card from a deck of cards = ", rndm_crd)

Output:

Enter some random List Elements separated by spaces = A K Q J 2 3 4 5 6 7 8 9 10
Enter some random List Elements separated by spaces = heart club spade diamond
The random card from a deck of cards = ('6', 'club')

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.