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
Related DS Analogue of Card Deck: Feature Bundle Generator · Data Science Engineering
Tasks
Question 1

Why is Card.FACES best represented as a class constant?

Hint

One shared mapping for all cards.

Question 2

Code task: implement Deck.deal with input validation.

import random

class Deck:
    def __init__(self, cards):
        self.cards = cards

    def deal(self, n):
        # TODO
        pass

Raise ValueError for invalid n.

Submission format: submit the full class snippet shown above with # TODO/pass replaced.

Hint

Use bounds check and random.sample.

Starter code is prefilled; replace TODO blocks with your solution.
2 test cases will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Question 3

For draw-without-replacement behavior, which standard function matches the requirement directly?

Hint

You need unique picks from a population.

Question 4

Code task: implement build_standard_cards() returning all 52 (rank, suit) tuples for ranks 2..14 and suits Clubs, Diamonds, Hearts, Spades.

Submission format: submit a full function definition def build_standard_cards(...): ....

Hint

Use nested loops or itertools.product.

Starter code is prefilled; replace TODO blocks with your solution.
2 test cases will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Python OOP Inheritance
  • Difficulty: Beginner
  • Completed: 2 users
Creator
Pavel
Pavel