> For the complete documentation index, see [llms.txt](https://digitalgarden.batamladen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://digitalgarden.batamladen.com/notes/programming/python/examples-projects/blackjack-game.md).

# BlackJack Game

{% tabs %}
{% tab title="main" %}

```python
from replit import clear
from art import logo
import random

def blackjack_game():

    def start_deal(cards):
        shuffle = random.choice(cards)
        return shuffle

    def player_loss(player, cpu):
        if sum(player) != 21 and sum(cpu) == 21:
            return True
        elif sum(player) == 21 and sum(cpu) == 21:
            return True

    def done_playing():
        player_sum = sum(player_cards)
        cpu_sum = sum(cpu_cards)
        print(f"Your hand is: {player_cards}, a total of {player_sum}")
        print(f"Comp hand is: {cpu_cards}, a total of {cpu_sum}")

        if sum(cpu_cards) > 21:
            print("The comp got a burst you win!")
        elif player_loss(player_cards, cpu_cards):
            print("You lose.")
        elif sum(player_cards) == 21 and sum(cpu_cards) != 21:
            print("You win!")
        elif sum(player_cards) > 21:
            print("It's a burs you lose.")
        elif player_sum == cpu_sum:
            print("You win!")
        else:
            print("You lose!")

        play_again = input("Do you want to play again? (y / n): ")

        if play_again == 'y':
            clear()
            blackjack_game()
        else:
            if play_again == 'n':
                print("Goodbye.")


    def keep_playing():
        shuffled = start_deal(cards)
        player_cards.append(shuffled)
        cpu_cards.append(start_deal(cards))
        player_sum = sum(player_cards)
        cpu_sum = sum(cpu_cards)

        if 11 in player_cards and player_sum >= 21:
            ace = player_cards.index(11)
            player_cards[ace] = 1
            player_sum = sum(player_cards)
        print(f"Your hand is: {player_cards}, a total of {player_sum}.")
        print(f"The computer's hand is {cpu_cards}, a total of {cpu_sum}.")
        if player_sum > 21:
            print("It's a bust, you lose.")
            play_again = input("Do you want to play again? Type 'y' or 'n': ")
            if play_again == 'y':
                clear()
                blackjack_game()
            elif play_again == 'n':
                print("Thanks for playing. Goodbye.")
        if player_sum <= 21:
            hit = input("Type 'y' to get another card, type 'n' to pass: ")
            if hit == 'y':
                keep_playing()
            elif hit == 'n':
                done_playing()


    print(logo)
    cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
    player_cards = []
    cpu_cards = []
    player_cards.append(start_deal(cards))
    player_cards.append(start_deal(cards))
    cpu_cards.append(start_deal(cards))

    print(f"Your cards: {player_cards}")
    print(f"The computer's first card: {cpu_cards}")
    hit = input("Type 'y' to get another card, type 'n' to pass: ")

    if hit == 'n':
        done_playing()
    elif hit == 'y':
        keep_playing()

blackjack_game()
```

{% endtab %}

{% tab title="art" %}

```python
logo =  """
.------.            _     _            _    _            _    
|A_  _ |.          | |   | |          | |  (_)          | |   
|( \/ ).-----.     | |__ | | __ _  ___| | ___  __ _  ___| | __
| \  /|K /\  |     | '_ \| |/ _` |/ __| |/ / |/ _` |/ __| |/ /
|  \/ | /  \ |     | |_) | | (_| | (__|   <| | (_| | (__|   < 
`-----| \  / |     |_.__/|_|\__,_|\___|_|\_\ |\__,_|\___|_|\_\\
      |  \/ K|                            _/ |                
      `------'                           |__/           
"""
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://digitalgarden.batamladen.com/notes/programming/python/examples-projects/blackjack-game.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
