Open In App

Shuffle a deck of card with OOPS in Python

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The objective is to distribute a deck of cards among two players. The code for the Shuffle Deck of Cards in Python can be used to shuffle the cards. The shuffle method, which is a built-in feature of the random library, is used to mix and randomize the order of the data before printing it.

Prerequisites: Python Classes and Objects

Steps to Shuffle Deck of Cards

  • To shuffle the deck of cards we need to use the shuffle module.
  • Import the required module
  • Declare a class named Cards which will have variables suites and values, now instead of using self.suites and self.values, we are going to declare them as global variables.
  • Declare a class Deck that will have an empty list named mycardset, and the suites and values will be appended to mycardset  list.
  • Declare a class ShuffleCards along with a method named shuffle() that would check the number of cards and then shuffles them.
  • To remove some cards we will create a popCard() method in ShuffleCards class.

Complete Code

Shuffle a deck of cards using a Python random module.

Python3




# Import required modules
from random import shuffle
 
 
# Define a class to create
# all type of cards
class Cards:
    global suites, values
    suites = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
    values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
 
    def __init__(self):
        pass
 
 
# Define a class to categorize each card
class Deck(Cards):
    def __init__(self):
        Cards.__init__(self)
        self.mycardset = []
        for n in suites:
            for c in values:
                self.mycardset.append((c)+" "+"of"+" "+n)
 
    # Method to remove a card from the deck
    def popCard(self):
        if len(self.mycardset) == 0:
            return "NO CARDS CAN BE POPPED FURTHER"
        else:
            cardpopped = self.mycardset.pop()
            print("Card removed is", cardpopped)
 
 
# Define a class gto shuffle the deck of cards
class ShuffleCards(Deck):
 
    # Constructor
    def __init__(self):
        Deck.__init__(self)
 
    # Method to shuffle cards
    def shuffle(self):
        if len(self.mycardset) < 52:
            print("cannot shuffle the cards")
        else:
            shuffle(self.mycardset)
            return self.mycardset
 
    # Method to remove a card from the deck
    def popCard(self):
        if len(self.mycardset) == 0:
            return "NO CARDS CAN BE POPPED FURTHER"
        else:
            cardpopped = self.mycardset.pop()
            return (cardpopped)
 
 
# Driver Code
# Creating objects
objCards = Cards()
objDeck = Deck()
 
# Player 1
player1Cards = objDeck.mycardset
print('\n Player 1 Cards: \n', player1Cards)
 
# Creating object
objShuffleCards = ShuffleCards()
 
# Player 2
player2Cards = objShuffleCards.shuffle()
print('\n Player 2 Cards: \n', player2Cards)
 
# Remove some cards
print('\n Removing a card from the deck:', objShuffleCards.popCard())
print('\n Removing another card from the deck:', objShuffleCards.popCard())


Output:

Shuffle Deck of Cards in Python

 



Previous Article
Next Article

Similar Reads

How to Print a Deck of Cards in Python
This article teaches how to Print a Deck of Cards using Python. The most popular deck of cards used today is a normal 52-card deck of French-suited playing cards. It is the only traditional pack[b] used for playing cards in English-speaking nations, but in many other nations throughout the world, it is used alongside other traditional, frequently o
5 min read
Python | oops | Question 12
What is the Diamond Problem of Inheritance? (A) A problem where a class inherits from multiple superclasses with conflicting methods (B) A problem where a class has multiple instances with different states (C) A problem where a class has circular references and cannot be instantiated (D) A problem where a class has a complex inheritance hierarchy w
1 min read
Python | oops | Question 3
Which of the following is associated with objects? (A) State (B) Behavior (C) Identity (D) All of the above Answer: (D)Explanation: All the above concepts are associated with object. Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
__invert__ and __abs__ magic functions in Python OOPS
The __invert__ and __abs__ magic methods are used for implementing unary operators in Python. In this article, we will see the concept of __invert__ and __abs__ magic functions in Python. __invert__() Magic Function in PythonThe __invert__ magic function is used to define the behavior of the bitwise NOT operation (~). When this method is implemente
4 min read
Python OOPS - Aggregation and Composition
In this article, we will compare and highlight the features of aggregation and Composition in Python OOPS. Concept of Inheritance Inheritance is a mechanism that allows us to take all of the properties of another class and apply them to our own. The parent class is the one from which the attributes and functions are derived (also called as Base Cla
5 min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles—classes, objects, inheritance, encapsulation, polymorphism, and abstraction—programmers can leverage the full potential of Python's OOP capabilities to design elega
12 min read
Python | Ways to shuffle a list
In Python, Shuffling a sequence of numbers has always been a useful utility and the question that has appeared in many company placement interviews as well. Knowing more than one method to achieve this can always be a plus. Let's discuss certain ways in which this can be achieved. Python Random Shuffle a ListIn Python, there are several ways to shu
5 min read
Python | Shuffle two lists with same order
Sometimes, while working with Python list, we can have a problem in which we need to perform shuffle operation in list. This task is easy and there are straightforward functionalities available in Python to perform this. But sometimes, we need to shuffle two lists so that their shuffle orders are consistent. Let's discuss a way in which this task c
3 min read
random.shuffle() function in Python
The shuffle() is an inbuilt method of the random module. It is used to shuffle a sequence (list). Shuffling a list of objects means changing the position of the elements of the sequence using Python. Syntax of random.shuffle() The order of the items in a sequence, such as a list, is rearranged using the shuffle() method. This function modifies the
2 min read
numpy.random.shuffle() in python
With the help of numpy.random.shuffle() method, we can get the random positioning of different integer values in the numpy array or we can say that all the values in an array will be shuffled randomly. Syntax : numpy.random.shuffle(x) Return : Return the reshuffled numpy array. Example #1 : In this example we can see that by using numpy.random.shuf
1 min read
Practice Tags :
three90RightbarBannerImg