I made a simple blackjack with Python

Introduction

This article is written by a beginner who has been programming for a month and a half, so please take a warm look.

environment ・ Windows 10 (can be done on Mac at all)

Language used ・ Python3

Editor to use ・ VScode

Sample code

blackjack.py



import random


deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] * 4


def deal():
    hand = []
    for i in range(2):
        random.shuffle(deck)
        card = deck.pop()
        if card == 11:
            card = "J"
        if card == 12:
            card = "Q"
        if card == 13:
            card = "K"
        if card == 1:
            card = "A"
        hand.append(card)
    return hand


def hit(hand):
    random.shuffle(deck)
    card = deck.pop()
    if card == 11:
        card = "J"
    if card == 12:
        card = "Q"
    if card == 13:
        card = "K"
    if card == 1:
        card = "A"
    hand.append(card)
    return hand


def total(hand):
    score = 0
    for card in hand:
        if card == "J" or card == "Q" or card == "K":
            score = score + 10
        elif card == "A":
            if score >= 11:
                score = score + 1
            else:
                score += 11
        else:
            score += card
    return score


def play_again():
    again = input("Would you like to play again?(Y/N): ")
    if again == "y" or again == "Y":
        # game()
        return
    else:
        print("Thank you for your hard work!")
        exit()


def result(dealer_hand, player_hand):
    if total(player_hand) > total(dealer_hand):
        print(
            f"\n Dealer total{total(dealer_hand)}Your total{total(player_hand)}is.\033[32mYOU WIN!\033[0m")
    elif total(dealer_hand) > total(player_hand):
        print(
            f"\n Dealer total{total(dealer_hand)}Your total{total(player_hand)}is.\033[91mYOU LOSE...\033[0m")


def game():
    dealer_hand = deal()
    player_hand = deal()
    print(f"The dealer's card is{dealer_hand[0]}is.")
    print(f"The player's card is{player_hand}The total is{total(player_hand)}is.")

    choice = 0

    while choice != quit:
        choice = input("Do you want to hit? Do you want to stand?(HIT/STAND): ").lower()
        if choice == "hit":
            hit(player_hand)
            print(
                f"\n to you{player_hand[-1]}Is dealt and the card is{player_hand}The total is{total(player_hand)}is.")
            if total(player_hand) > 21:
                print("You have exceeded 21.\033[91mYOU LOSE...\033[0m")
                choice = quit

        elif choice == "stand":
            print(
                f"\n The dealer's second card is{dealer_hand[1]}The total is{total(dealer_hand)}is.")
            while total(dealer_hand) < 17:
                hit(dealer_hand)
                print(
                    f"To the dealer{dealer_hand[-1]}Is dealt and the card is{dealer_hand}The total is{total(dealer_hand)}is.")
                if total(dealer_hand) > 21:
                    print("The number of dealers has exceeded 21.\033[32mYOU WIN!\033[0m")
                    choice = quit

            if total(dealer_hand) <= 21:
                result(dealer_hand, player_hand)
                choice = quit


game()

If you press it in the terminal, the game will start.

Recommended Posts

I made a simple blackjack with Python
I made blackjack with Python.
I made a fortune with Python.
I made a daemon with Python
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a simple typing game with tkinter in Python
I made a simple book application with python + Flask ~ Introduction ~
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I made a simple Bitcoin wallet with pycoin
[Python] I made an image viewer with a simple sorting function.
I made a python text
I made wordcloud with Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Twitter BOT with GAE (python) (with a reference)
I made a Christmas tree lighting game with Python
I made a net news notification app with Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
I made a Line-bot using Python!
I made a package to filter time series with python
I made a puzzle game (like) with Tkinter in Python
A simple RSS reader made with Django
I made a payroll program in Python!
I tried a functional language with Python
What I did with a Python array
I made a life game with Numpy
I made a stamp generator with GAN
After studying Python3, I made a Slackbot
I made a WEB application with Django
I made a library to easily read config files with Python
I made a Nyanko tweet form with Python, Flask and Heroku
I made a lot of files for RDP connection with Python
I made a shuffle that can be reset (reverted) with Python
I made a poker game server chat-holdem using websocket with python
I made a segment tree with python, so I will introduce it
I made a stamp substitute bot with line
I made a python dictionary file for Neocomplete
I want to make a game with Python
Procedure for creating a LineBot made with Python
Start a simple Python web server with Docker
[Python] Make a simple maze game with Pyxel
I made a LINE Bot with Serverless Framework!
A simple to-do list created with Python + Django
I made a random number graph with Numpy
I want to write to a file with Python
I made a simple RSS reader ~ C edition ~
I made a Caesar cryptographic program in Python.
I made a Python Qiita API wrapper "qiipy"
I made a QR code image with CuteR
I made a tool to automatically browse multiple sites with Selenium (Python)
I tried to discriminate a 6-digit number with a number discrimination application made with python