Yes
Yes

Reputation: 31

Give variables 2 values

import random, time

from enum import Enum

class CardSuits(Enum):
    Hearts = "Hearts"
    Diamonds = "Diamonds"
    Clubs = "Clubs"
    Spades = "Spades"

class CardValues(Enum):
    Two = 2
    Three = 3
    Four = 4
    Five = 5
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = "J"
    Queen = "Q"
    King = "K"
    Ace = "A"


class Card:

    _symbols = {"Hearts": "♥️", "Diamonds": "♦️", "Clubs": "♣️", "Spades": "♠"}

    def __init__(self, suit: CardSuits, value: CardValues) -> None:
        self.suit = suit
        self.value = value

    def __str__(self) -> str:
        return f"{self.value.name}{self._symbols[self.suit.name]}"

    def __repr__(self) -> str:
        return f"{self.value.name}{self._symbols[self.suit.name]}"


def start():
    play = input("You wanna play? ")
    if play.lower() in ("yes", "sure", "yeah"):
        unique_cards = set()
        while len(unique_cards) < 5:
            unique_cards.add(Card(random.choice(list(CardValues)), random.choice(list(CardSuits))))
        cards = list(unique_cards)
        print("Your cards are:", ", ", cards)
        time.sleep(4)
        

To use it you create individual cards via:

card = Card(CardSuits.Clubs, CardValues.Ace)

With this you mean that I have to create every single card of the deck or is it like a sample where all other cards will get the "creation way"? It keeps telling me there's an error there: {self._symbols[self.suit.name]}"

And also, up to this point is all right? I modified a little bit the first part, when it will be ok, I'll modify the rest fo the code based on it.

Upvotes: 0

Views: 131

Answers (1)

Oskar Hofmann
Oskar Hofmann

Reputation: 807

You are currently handling your cards simply as strings. As each card has two separate properties (value and suit) I propose you define a Card class. This allows you to easily retrieve and change the value and suit of a class and also define a way on how to print a card.

One possible example would be something like

from enum import Enum, auto

class CardSuits(Enum):
    Clubs = auto() # 1
    Spades = auto() # 2
    Hearts = auto() # 3
    Diamonds = auto() # 4

class CardValues(Enum):
    Ace = auto() # 1
    Deuce = auto() # 2
    Three = auto() # 3
    Four = auto() # 4
    Five = auto() # 5
    Six = auto() # 6
    Seven = auto() # 7
    Eight = auto() # 8
    Nine = auto() # 9
    Ten = auto() # 10
    Jack = auto() # 11
    Queen = auto() # 12
    King = auto() # 13

class Card:
    def __init__(self, suit: CardSuits, value: CardValues) -> None:
        self.suit = suit
        self.value = value

    def __str__(self) -> str:
        return f"{self.value.name} of {self.suit.name}"

    def __repr__(self) -> str:
        return f"{self.value.name} of {self.suit.name}"

You do not need to define the two Enum-classes but it really helps when you often deal with these properties in your card game. The code above is from a blackjack game I wrote for myself, if you are interested in further references on how to use this class.

To use it you create individual cards via:

card = Card(CardSuits.Clubs, CardValues.Ace)

And you can redefine the __str__ (and __repr__) function if you prefer to use symbols for printing to the console, e.g.:

class Card:

    _symbols = {"Hearts": "♥️", "Diamonds": "♦️", "Clubs": "♣️", "Spades": "♠"}

    def __init__(self, suit: CardSuits, value: CardValues) -> None:
        self.suit = suit
        self.value = value

    def __str__(self) -> str:
        return f"{self.value.name} of {self._symbols[self.suit.name]}"

    def __repr__(self) -> str:
        return f"{self.value.name} of {self._symbols[self.suit.name]}"

Upvotes: 2

Related Questions