From d86423b3c5bd49f3dd21e2d10772475967309a3a Mon Sep 17 00:00:00 2001 From: Donald Calloway Date: Wed, 17 Sep 2025 10:21:27 -0700 Subject: [PATCH] Updated deal_card function to eliminate ValueError exception --- poker.py | 76 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/poker.py b/poker.py index f188e1e..162db60 100644 --- a/poker.py +++ b/poker.py @@ -14,12 +14,12 @@ deck: Deck new_card: Card def create_deck() -> Deck: - deck: Deck = [(rank, suit) for suit in SUITS for rank in RANKS] - return deck + Deck = [(rank, suit) for suit in SUITS for rank in RANKS] + return Deck -def deal_card(deck: Deck) -> Card: - pull_card = pop_list(deck, 0) - deck.remove(pull_card) +def deal_card(deck) -> Card: + pull_card = deck.pop(0) + print("DEBUG - Dealt card:", pull_card) return pull_card def deal_hand(deck, num=5) -> Hand: @@ -36,6 +36,13 @@ def pop_list(lst: List, index: int) -> Tuple: lst.remove(element) return element +def card_in_hand(card: Card, hand: Hand) -> bool: + rank, suit = card + for r, s in hand: + if r.strip().upper() == rank.strip().upper() and s.strip().capitalize() == suit.strip().capitalize(): + return True + return False + def hand_rank(hand): ranks = sorted([RANK_VALUES[card[0]] for card in hand], reverse=True) suits = [card[1] for card in hand] @@ -65,30 +72,48 @@ def hand_rank(hand): def show_hand(hand): return '\n '.join([f"{rank} of {suit}" for rank, suit in hand]) -def replace_card_from_hand(hand, card, new_card) -> bool: - if card in hand: - hand.remove(card) - hand.append(new_card) - return True - return False +def replace_card_from_hand(hand: Hand, card: Card, new_card: Card) -> bool: + for i, existing_card in enumerate(hand): + if existing_card[0].strip().upper() == card[0].strip().upper() and \ + existing_card[1].strip().capitalize() == card[1].strip().capitalize(): + hand[i] = new_card + return True -def handle_player_input(hand): - user_input = input("Player, enter a card to pull from your hand (e.g., A of Spades) ") + +def handle_player_input(hand: Hand, deck: Deck) -> None: + user_input = input("Enter a card to pull from your hand (e.g., A of Spades): ").strip() + print("DEBUG - Current hand:", hand) + # Basic format check + + if ' of ' not in user_input: + print("Invalid input format. Please use Rank of Suit.") + return try: - rank, suit = user_input.split(' of ') - card_to_replace = (rank.strip().upper(), suit.strip().capitalize()) - new_card = deal_card(Deck) # Example new card from the deck + rank, suit = user_input.split(' of ', 1) + rank = rank.strip().upper() + suit = suit.strip().capitalize() + card = (rank, suit) + print("DEBUG - Attempting to remove:", card) + if not card_in_hand(card, hand): + print("DEBUG - Normalized input:", card) + print("DEBUG - Hand contents:", hand) + print(f"Card '{rank} of {suit}' not found in your hand.") + return + + if not deck: + print("No more cards left in the deck to draw.") + return + + new_card = deal_card(deck) + success = replace_card_from_hand(hand, card, new_card) - success = replace_card_from_hand(hand, card_to_replace, new_card) - print(success) if success: - print(f"Player 1 pulled {rank} of {suit} from their hand.") - print(f"Player 1 received new card: {new_card[0]} of {new_card[1]}") + print(f"Replaced {rank} of {suit} with {new_card[0]} of {new_card[1]}.") else: - print("Card not found in Player 1's hand.") - except ValueError: - print("Invalid input format. Please use 'Rank of Suit'.") + print("Card replacement failed unexpectedly.") + except Exception as e: + print(f"Unexpected error: {e}") def display_initial_hands(player1: Hand, player2: Hand): print("\nPlayer's hands:") @@ -107,6 +132,7 @@ def display_hands(player1: Hand, player2: Hand): def main(): deck = create_deck() random.shuffle(deck) + random.shuffle(deck) player1 = deal_hand(deck) player2 = deal_hand(deck) display_initial_hands(player1, player2) @@ -117,7 +143,7 @@ def main(): return elif p1_num != 0: for _ in range(p1_num): - handle_player_input(player1) + handle_player_input(player1, deck) else: print("Player 1 chose not to pull any cards.") display_hands(player1, player2) @@ -129,7 +155,7 @@ def main(): return elif p2_num != 0: for _ in range(p2_num): - handle_player_input(player2) + handle_player_input(player2, deck) else: print("Player 2 chose not to pull any cards.") display_hands(player1, player2)