Day 02

Shubham TomarShubham Tomar
2 min read

Created a blackjack program in Python using 2 hints. Going through the 100 days of code challenge. Now I will see the complete video and see where I was wrong. It's going well within the next few weeks will start learning basic javascript and some CS concepts.

Here is the code :

import random

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
print(logo)

question = input("Would you like to play Blackjack?\nType Y if you do : ")
while question != 'y' or question != 'Y':
    if question == 'y' or question == 'Y':
        print("WELCOME TO BLACKJACK")
        break
    else:
        print("Restart the program when you wish to play.")
        break

dealer_cards = []
player_cards = []


def dealer():
    i = random.randrange(0, 13)
    dealer_cards.append(cards[i])

def player():
    j = random.randrange(0, 13)
    player_cards.append(cards[j])

def calculate():

    for i in range(0, len(dealer_cards)):
        if sum(dealer_cards) > 21 and dealer_cards[i] == 11:
            dealer_cards[i] = 1


    for j in range(0, len(player_cards)):
        if sum(player_cards) > 21 and player_cards[j] == 11:
            player_cards[j] = 1



def blackjack():
    dealer()
    dealer()
    print(f"Dealer Cards : {dealer_cards[0]}")

    player()
    player()
    print(f"Player Cards : {player_cards}, Total : {sum(player_cards)}")

    isGame = True

    while isGame:
        question = input("Do you wish to continue?\nType y : ")
        if question == 'y' or question == 'Y':
            player()
            print(f"Dealer Cards : [{dealer_cards[0]}]")
            print(f"Player Cards : {player_cards}, Total : {sum(player_cards)}")
            calculate()
        elif question == 'n' or question == 'N':
            calculate()
            while sum(dealer_cards) < 17:
                dealer()
            print(f"Dealer Cards : {dealer_cards}, Total : {sum(dealer_cards)}")
            print(f"Player Cards : {player_cards}, Total : {sum(player_cards)}")

            isGame = False    



    if sum(player_cards) > 21:
        print("Dealer Win")
    elif sum(player_cards) <= 21 and sum(player_cards) < sum(dealer_cards):
        print("You Win")
    elif sum(player_cards) == sum(dealer_cards):
        print("Its a Draw")
    elif sum(dealer_cards) <= 21:
        print('Dealer Win')
    else:
        print("You Lose")



blackjack()
0
Subscribe to my newsletter

Read articles from Shubham Tomar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shubham Tomar
Shubham Tomar