import random, timefrom random import shufflefrom enum import Enumclass 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.value}{self._symbols[self.suit.name]}" def __repr__(self) -> str: return f"{self.value.value}{self._symbols[self.suit.name]}"def start(): play = input("You wanna play? ") if play.lower() in ("yes", "sure", "yeah"): all_cards = [Card(suits, value) for suits in CardSuits for value in CardValues] shuffle(all_cards) unique_cards = set() while len(unique_cards) < 5: unique_cards.add(random.choice(all_cards)) cards = list(unique_cards) print("Your cards are:", ", ".join(str(card) for card in cards)) time.sleep(4) while True: change_cards = input("Want to change cards? ") if change_cards.lower() == "yes": quantity = input("Which cards? ") first = cards[0] second = cards[1] third = cards[2] fourth = cards[3] fifth = cards[4] first, second, third, fourth, fifth = ("first", "second", "third", "fourth", "fifth") def print_new_cards(): print("Your new cards are:", ", ".join(str(card) for card in cards)) if quantity.lower() == "none": break elif quantity.lower() == first: cards[0] = random.choice(all_cards) print_new_cards() break elif quantity.lower() == second: cards[1] = random.choice(all_cards) print_new_cards() break elif quantity.lower() == third: cards[2] = random.choice(all_cards) print_new_cards() break elif quantity.lower() == fourth: cards[3] = random.choice(all_cards) print_new_cards() break elif quantity.lower() == fifth: cards[4] = random.choice(all_cards) print_new_cards() break elif quantity.lower() == "all": all_cards = [Card(suits, value) for suits in CardSuits for value in CardValues] unique_cards = set() while len(unique_cards) < 5: unique_cards.add(random.choice(all_cards)) cards = list(unique_cards) print("Your new cards are:", ", ".join(str(card) for card in cards)) break else: print("Invalid cards selected") elif change_cards.lower() == "no": break #CHECKING PHASE #SCORE DASHBOARD? while True: continue_playing = input("You want to keep playing?") if continue_playing.lower() == "yes": wanna_shuffle = input("Wanna shuffle the deck?") if wanna_shuffle.lower() == "yes": shuffle(all_cards) print("Deck shuffled") break elif continue_playing.lower() == "no": break else: continue elif continue_playing.lower() == "no": exit() start() else: exit()start()
the question is: how can i write a few simple lines of code to change cards by the index without writing every change possibility as I was doing?
My inital idea was to change them by writing in the terminal which cards i would change.
ex. hand: 5♦️, A♦️, K♠, J♣️, 2♦️
n the terminal:
Want to change cards?
yes
Which cards?
K spades, J clubs
Your new cards are: 5♦️, A♦️, 6♥️, 7♥️, 2♦️
But i realized that for me is a little too difficult, so I changed my mind and decided to change cards by writing the index of them.
ex. hand: 5♦️, A♦️, K♠, J♣️, 2♦️
Which cards?
3, 4 or 3rd, 4rth or third, fourth
Your new cards are: 5♦️, A♦️, 9♠, Q♣️, 2♦️