top of page

Making a Blackjack game

  • Writer: 라임 샹큼
    라임 샹큼
  • Feb 28
  • 2 min read

Blackjack is a card game where you are given 2 cards and either hold or choose new cards depending on whether you feel you are closer to 21. If your held cards are bigger than the opposing side's cards, you win.


import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]


card ={

    'computer': [],

    'you':[]

}

def pick():



    card['you'].append(random.choice(cards))

    card['computer'].append(random.choice(cards))

    if sum(card['you']) > 21:

        print(f'BUST.your cards are {card["you"]}. your cards are above 21')

        choice3 = input('type y to restart game, type n to exit ').lower()

        return 'bust'

    else:

        print(f'your cards are {card["you"]}')

        print(f'computers cards are {card["computer"][0:len(card["computer"])-1]}')



def hold():

    print(f'computers cards were {card["computer"]}')

    if sum(card['computer']) < sum(card['you']) <= 21:

        print('You won!!')

    elif sum(card['computer']) == sum(card['you']):

        print('It\'s a tie!')

    elif sum(card['you']) < sum(card['computer']) <= 21:

        print('You lost...')

    elif sum(card['computer']) > 21:

        print('Computer overrode 21, you won!')

    choice3 = input('type y to restart game, type n to exit ').lower()

    if choice3 == 'y':

        card['computer'] = []

        card['you'] = []

        black()

    else:

        exit()



def black():

    print()

    choice3 = 'y'

    while choice3 == 'y':

        for e in range(2):

            for element in card:

                card[element].append(random.choice(cards))

        print(f'your cards are {card["you"]}')

        print(f'computers 1st card is {card["computer"][0]}')

        choice = input('Will you hold or pick?').lower()


        if choice == 'hold':

            hold()

        elif choice == 'pick':

            while choice == 'pick':

                if pick() == 'bust':

                    break

                else:

                    choice = input('Will you hold or pick?').lower()

            if len(card['you']) < 21:

                hold()

            choice3 = input('type y to restart game, type n to exit ').lower()



black()


Recent Posts

See All
Reading and writing files

I learned how to read write txt files. I learned how to navigate files. I always wondered how people sent automated emails to so many...

 
 
 
Ping-Pong game

This project really helped me understand OOP more and grasp the concept of inheritance in classes. screen = Screen() screen.setup(height...

 
 
 
Making the well-known Snake game

Everyone has atleast had one instance in which they downloaded snake.io on their phone and had a few goes at it. I wanted to replicate...

 
 
 

Comments


© 2024 by GifTED. Powered and secured by Wix

bottom of page