import random import time dealerstand = False playerstand = False def card(): value = random.randint(2, 14) if value > 11: value = 10 return value # Dealer's starting hand dealer_shown = card() print() print("The dealer draws 2 cards...") print("The dealer reveals a", dealer_shown) dealer_hand = dealer_shown + card() print() time.sleep(1) # Player's starting hand player_hand = card() print("You draw 2 cards...") print("Your hand:", player_hand) player_hand = player_hand + card() if player_hand == 22: player_hand = 12 print("Your hand:", player_hand) time.sleep(2) # Gameplay loop while player_hand < 21 and dealer_hand < 21: if playerstand == False: print() print("Hit or Stand?") player_choice = str.lower(input()) # Player chooses hit if player_choice == "hit" or player_choice == "h": print() drawncard = card() if drawncard == 11 and drawncard + player_hand < 21: drawncard = 1 player_hand = player_hand + drawncard time.sleep(1) print("You draw a {}".format(drawncard)) print("Your hand is now {}".format(player_hand)) # Player chooses stand/Invalid choice else: print() time.sleep(1) print("You choose to stand") playerstand = True # Dealer's turn if dealer_hand < 17: print() print("The dealer draws a card...") drawncard = card() if drawncard == 11 and dealer_hand + drawncard > 21: drawncard = 1 dealer_hand = dealer_hand + drawncard else: print() print("The dealer stands") dealerstand = True if dealerstand == True and playerstand == True: time.sleep(1) print() print("Your hand was {}".format(player_hand)) time.sleep(1) print("the dealer's hand was {}".format(dealer_hand)) if player_hand > dealer_hand: print("player win") if dealer_hand > player_hand: print("dealer win") if player_hand == dealer_hand: print("tie") break # Win conditions if player_hand > 21: print() print("dealer wins") print("Your hand was {}".format(player_hand)) print("Dealer's hand was {}".format(dealer_hand)) if dealer_hand > 21: print() print("player wins") print("Your hand was {}".format(player_hand)) print("Dealer's hand was {}".format(dealer_hand)) if player_hand == 21: print() print("player wins") print("Your hand was {}".format(player_hand)) print("Dealer's hand was {}".format(dealer_hand)) if dealer_hand == 21: print() print("dealer wins") print("Your hand was {}".format(player_hand)) print("Dealer's hand was {}".format(dealer_hand))