import time import random while True: playerscore = 0 dealerscore = 0 print("Welcome to the 21 Card Game!") tutorial = input("Would you like to learn how to play? Press 'y' and then Enter if yes: ") if tutorial == "y": print("The aim of the 21 Card game (21 for short) is to reach a sum total of 21!") time.sleep(1) print("All cards are at face value, but picture cards like Queen, King, or Jack are valued at 10!") time.sleep(1) print("Aces are initially valued at 11. If the total value exceeds 21, Ace will be counted as 1.") time.sleep(1) print("When you stand, it means you can't change your current sum. When you 'hit', you pick up another card, but be careful not to go over 21!") time.sleep(1) print("Good luck and Have Fun!") again = "y" while again == "y": player_cards = [] dealer_cards = [] player_cards.extend(random.choice([2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 11]) for _ in range(2)) dealer_cards.extend(random.choice([2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 11]) for _ in range(2)) def calculate_sum(cards): sum_val = sum(cards) num_aces = cards.count(11) while sum_val > 21 and num_aces: sum_val -= 10 num_aces -= 1 return sum_val playersum = calculate_sum(player_cards) dealersum = calculate_sum(dealer_cards) print("Dealer draws:") for i in range(2): print(f"Card {i+1}:", dealer_cards[i]) time.sleep(1) while dealersum < 16 and len(dealer_cards) < 10: new_card = random.choice([2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 11]) dealer_cards.append(new_card) dealersum = calculate_sum(dealer_cards) print(f"Card {len(dealer_cards)}:", new_card) time.sleep(1) print("Dealer's sum:", dealersum) time.sleep(1) print("You draw:") for i in range(2): print(f"Card {i+1}:", player_cards[i]) time.sleep(1) while playersum < 21: hit = input("Hit or Stand? (h/s) ") if hit == "h": new_card = random.choice([2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 11]) player_cards.append(new_card) playersum = calculate_sum(player_cards) print("You draw:", new_card) print("Sum =", playersum) time.sleep(1) if playersum > 21: print("Player busts!") break else: break if playersum <= 21: print("Dealer's turn:") print("Dealer's cards:", dealer_cards) print("Dealer's sum:", dealersum) time.sleep(1) if dealersum > 21: print("Dealer busts!") playerscore += 1 else: if playersum > dealersum: print("Player wins!") playerscore += 1 elif playersum < dealersum: print("Dealer wins!") dealerscore += 1 else: print("It's a tie!") else: if dealersum <= 21: print("Player busts!") dealerscore += 1 else: print("Both player and dealer busts!") print("Player score:", playerscore) print("Dealer score:", dealerscore) again = input("Play again? (y/n)") if again != "y": break