DEV Community

Reshma
Reshma

Posted on • Edited on

Black Jack game in python


import random
cards = [11,2,3,4,5,6,7,8,9,10,10,10,10]
divider = """________________________________________________________________________"""

def blackjack():

    # user drawing first two cards
    usercard1 = random.choice(cards)
    usercard2 = random.choice(cards)
    usercards = [usercard1,usercard2]
    usersum = usercard1 + usercard2
    print(f"your cards: {usercards}")



    #computer drawing a card
    dealercard = random.choice(cards)
    dealersum = dealercard
    print(f"Dealer card: {[dealercard]}")

    flag = True

    while flag:
        if usersum == 21:
            print("You win")
            flag = False
        elif usersum > 21:
            print("Bust")
            flag = False
        choice = input("Type 'h' to hit and 's' to stand: ").lower()

        if choice == 'h':
            #user draws another card

            usercard3 = random.choice(cards)
            usersum += usercard3
            print(f"Your total sum is : {[usersum]} ")
            if usersum > 21:
                print("Bust")
                flag = False
            elif usersum == 21:
                print("You win")
                flag = False
            else:   
                flag = True

        else:

            while dealersum < 18:
                dealercard2 = random.choice(cards)
                dealersum +=  dealercard2
            print(f"Dealer's sum is : {[dealersum]} ")
            if dealersum > 21:
                print("You win")
                flag = False
            elif usersum > 21:
                print("Bust")
                flag = False
            elif usersum == 21:
                print("You win")
                flag = False
            elif dealersum > usersum:
                print("You lose")
                flag = False
            elif dealersum == usersum:
                print("Draw")
                flag = False
            else:
                flag = False



blackjack()
res = input("Type 'Y' to Start again and 'N' to exit: ").lower()
if res == 'y':
    print(divider)
    blackjack()
else:
    exit()




Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
shredwheat profile image
Peter Shinners

The odds of getting an Ace (11) is significantly higher than other cards. In reality the 10 should be the highest since it represents the Ten, Jack, Queen, and King cards.

Collapse
 
reshma_17 profile image
Reshma

I hadn’t noticed that. Thanks for pointing it out!