Python Poker Hand Code
import random
def create_deck():
suits = ['\u2660 ,', '\u2665 ,', '\u2666 ,', '\u2663 ,']
values = ['ACE of ', '2 of ', '3 of ', '4 of ', '5 of ', '6 of ', '7 of ', '8 of ', '9 of ', '10 of ', 'J of ', 'Q of ', 'K of ']
return [value + suit for suit in suits for value in values]
def deal(deck, num_cards):
return random.sample(deck, num_cards)
def replace_cards(deck, hand, discard_indices):
new_cards = deal(deck, len(discard_indices))
for i, index in enumerate(discard_indices):
hand[index] = new_cards[i]
return hand
def display(hand):
print("Your Hand:")
for card in hand:
print(card, end=' ')
print()
def play_game():
deck = create_deck()
while True:
hand = deal(deck, 5)
display(hand)
discard_indices = []
while len(discard_indices) < 4:
choice = input("Enter index of card to discard (1-5), or 0 to keep all: ")
if choice == '0':
break
discard_indices.append(int(choice) - 1)
hand = replace_cards(deck, hand, discard_indices)
display(hand)
if input("Would you like to play again? (y/n): ").lower() != 'y':
break
if __name__ == "__main__":
play_game()