Playing Cards with OOP: Card and Deck
Beginner
Python OOP Inheritance
Created by Pavel
· 12.03.2026 at 07:54 UTC
· 2 completed
Problem setup:
Build a reusable model of a standard card deck for simulation and probability tasks.
Model:
- Card stores rank and suit.
- Card.FACES maps ranks 11..14 to face names.
- Deck constructs all combinations of ranks and suits.
- Deck.deal(n) returns n unique random cards (no replacement).
Example:
class Card:
FACES = {11: 'Jack', 12: 'Queen', 13: 'King', 14: 'Ace'}
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
value = self.FACES.get(self.rank, self.rank)
return f"{value} of {self.suit}"
Use cases:
- Monte Carlo game simulations,
- teaching combinations and sampling,
- custom deck variants.
Edge cases:
- n < 0,
- n > deck size,
- malformed rank/suit definitions.
University approvals: 0
Related cards
Tasks
Card Info
- Topic: Python OOP Inheritance
- Difficulty: Beginner
- Completed: 2 users
Creator
Pavel