Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Make a code for blackjack on python

  1. User

    Make a code for blackjack on python

  2. Assistant

    Sure! Here's a code for a simplified version of the blackjack game in Python:

    import random
    
    def create_deck():
        deck = []
        for suit in ['Hearts', 'Diamonds', 'Clubs', 'Spades']:
            for value in range(1, 14):
                if value == 1:
                    deck.append(('Ace', suit, 11))
                elif value >= 10:
                    deck.append(('10', suit, 10))
                elif value == 11:
                    deck.append(('Jack', suit, 10))
                elif value == 12:
                    deck.append(('Queen', suit, 10))
                elif value == 13:
                    deck.append(('King', suit, 10))
                else:
                    deck.append((str(value), suit, value))
        return deck
    
    def deal_card(deck):
        card = random.choice(deck)
        deck.remove(card)
        return card
    
    def calculate_score(cards):
        score = sum([card[2] for card in cards])
        if score > 21 and any(card[0] == 'Ace' for card in cards):
            for card in cards:
                if card[0] == 'Ace' and card[2] == 11:
                    card = list(card)
                    card[2] = 1
                    cards = [card if card[0] == 'Ace' else card for card in cards]
                    score = sum([card[2] for card in cards])
                    if score <= 21:
                        break
        return score
    
    def display_hand(hand):
        for card in hand:
            print(f"{card[0]} of {card[1]}")
    
    def play_blackjack():
        print("Welcome to Blackjack!")
        deck = create_deck()
        random.shuffle(deck)
        player_hand = [deal_card(deck), deal_card(deck)]
        dealer_hand = [deal_card(deck)]
        game_over = False
    
        while not game_over:
            player_score = calculate_score(player_hand)
            dealer_score = calculate_score(dealer_hand)
    
            print("\nPlayer's cards:")
            display_hand(player_hand)
            print(f"Score: {player_score}")
    
            print("\nDealer's card:")
            print(dealer_hand[0][0])
    
            if player_score == 21 or dealer_score == 21 or player_score > 21:
                game_over = True
            else:
                should_continue = input("\nDo you want to draw another card? 'y' or 'n': ").lower()
                if should_continue == 'y':
                    player_hand.append(deal_card(deck))
                else:
                    game_over = True
    
        while dealer_score < 17 and dealer_score < player_score and player_score <= 21:
            dealer_hand.append(deal_card(deck))
            dealer_score = calculate_score(dealer_hand)
    
        print("\nPlayer's final hand:")
        display_hand(player_hand)
        print(f"Final Score: {player_score}")
        
        print("\nDealer's final hand:")
        display_hand(dealer_hand)
        print(f"Final Score: {dealer_score}")
    
        if player_score > 21:
            print("\nYou went over 21. You lose!")
        elif dealer_score > 21:
            print("\nThe dealer went over 21. You win!")
        elif player_score == dealer_score:
            print("\nIt's a draw!")
        elif player_score == 21:
            print("\nBlackjack! You win!")
        elif player_score > dealer_score:
            print("\nYou win!")
        else:
            print("\nYou lose!")
    
    play_blackjack()
    

    This code creates a simplified version of the blackjack game, where the player plays against the dealer. It starts by creating a deck of cards, and then deals two cards to the player and one card to the dealer. The player can then choose to draw more cards or stop. Once the player stops, the dealer draws cards until their score is at least 17 or they exceed the player's score. Finally, the scores are compared, and the winner or draw is announced.

    Note: This code does not handle all the rules and variations of blackjack, but it provides a basic gameplay.