Code a Game of Blackjack with Python

21
1



Learn how to code a command line game of Blackjack with the Python programming language.

## CHECK OUT THE FOLLOW ON VIDEO TO TURN THIS SAME CODE BASE INTO A BEST OF FIVE GAME

You know Blackjack. Also, referred to as 21.

In this game, you play against the dealer. You are both dealt hands and you can Hit or Stay based on the knowledge of your cards and the one card you can see of the dealers.

Welcome to Las Vegas or Atlantic City!! We are going to bring that to your Terminal 🙂

The source code for this program can be found here on Github

Other cool coding and tech things can be found here

The Python programming language can be found here

Winner, Winner, Chicken Dinner!! 🙂

Nguồn: https://khudothidongvan.com/

Xem thêm bài viết khác: https://khudothidongvan.com/game/

21 COMMENTS

  1. I like this for showing the process, but as far as a black jack game it has a lot of faults. for example the cards are not 1-11, they are 1/11, 2-10, 10, 10, 10. 13 options in all with one having two possible values. for a working on coding project, it's fine, but for a workable logic for blackjack, not so much, as the odds are entirely different here.

  2. i added

    if sum(dealer_cards) <= 16:
    dealer_cards.append(random.choice(deck))

    yet the dealer refuses to ever take a card. any way to fix this?

  3. I took the base and ran with it

    import random, time

    #blackjack

    #Planning

    player_win = 0

    player_lose = 0

    play_again = "Y"

    you_lose_automatically = "n"

    games_played = 0

    while play_again == "Y":

    games_played = games_played + 1

    # Dealer cards

    dealer_cards = []

    # player cards

    player_cards = []

    # deal the cards

    #dealer cards

    while len(dealer_cards) != 2:

    dealer_cards.append(random.randint(1,11))

    if len(dealer_cards) == 2:

    print("Dealer has X & ", dealer_cards[1])

    #player_cards

    while len(player_cards) != 2:

    player_cards.append(random.randint(1,11))

    if len(player_cards) == 2:

    print("You have " + str(player_cards[1]) + " & " + str(player_cards[0]) + " which equals " + str(sum(player_cards)))

    if sum(player_cards) < 21:

    action_taken = input("Do you want to stay or hit? " )

    while action_taken == "hit":

    new_card = random.randint(1,11)

    if new_card + sum(player_cards) >= 21:

    player_cards.append(new_card)

    action_taken = "stay"

    else:

    player_cards.append(new_card)

    print("You now have a total of " + str(sum(player_cards)) + " from these cards", player_cards)

    action_taken = input("Do you want to stay or hit? ")

    if sum(player_cards) == 21:

    print("Your cards are now " + str(player_cards) + " totaling " + str(sum(player_cards)) + ". Blackjack!")

    elif sum(player_cards) > 21:

    print("Your cards are now " + str(player_cards) + " totaling " + str(sum(player_cards)) + ". You bust!")

    else:

    print("You now have a total of " + str(sum(player_cards)) + " from these cards", player_cards)

    #dealer plays

    if sum(player_cards) > 21:

    print("n")

    print("You lose :(")

    you_lose_automatically = "y"

    else:

    print("Now the dealer plays")

    time.sleep(2)

    if sum(dealer_cards) > 17:

    print("Dealer has " + str(dealer_cards) + " totaling " + str(sum(dealer_cards)))

    else:

    print("Dealer has ",dealer_cards)

    dealer_hits = True

    while dealer_hits == True:

    print("Dealer hits..")

    new_dealer_card = random.randint(1,11)

    dealer_cards.append(new_dealer_card)

    time.sleep(1)

    print("Dealer cards are now " + str(dealer_cards) + " totaling " + str(sum(dealer_cards)))

    time.sleep(2)

    if sum(dealer_cards) < 17:

    dealer_hits = True

    else:

    dealer_hits = False

    # compare the sums of the cards between D v P

    # if P card sum is greater than 21 = Bust

    # if P card sum is less than 21 = option hit or stay

    # if p option stay compare sum of D v P

    # if P sum < 21 && > D sum = P Wins!

    # if P sum is < D sum = P loses

    if you_lose_automatically == "y":

    print("n")

    else:

    if sum(dealer_cards) > 21:

    print("Dealer busts!")

    elif sum(dealer_cards) == 21:

    print("Dealer has blackjack :O")

    else:

    print()

    if sum(dealer_cards) > 21:

    if sum(player_cards) <= 21:

    print("n")

    print("You win!")

    player_win = player_win+1

    else:

    print("n")

    print("You lose :(")

    player_lose = player_lose +1

    else:

    if sum(player_cards) == sum(dealer_cards):

    print("n")

    print("It's a draw.")

    player_win = player_win + 0

    player_lose = player_lose + 0

    else:

    if sum(player_cards) > 21:

    player_lose = player_lose + 1

    elif sum(player_cards) > sum(dealer_cards):

    print("n")

    print("You win!!")

    player_win = player_win + 1

    else:

    print("n")

    print("You lose :(")

    player_lose = player_lose + 1

    time.sleep(1)

    print("n")

    time.sleep(1)

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

    play_again = play_again.upper()

    print("Thanks for playing, you played " + str(games_played) + " games. You won " + str(player_win) + " time(s), and lost " + str(player_lose) + " time(s).")

    win_percentage = (player_win/games_played)*100

    win_percentage = round(win_percentage)

    print("Win percentage = " + str(win_percentage) + "%")

  4. thanks my brother

    i m french and i understand very well your fantastic course about blackjack python

    you are a great developper RESPECT

  5. import random

    card_deck =[2,3,4,5,6,7,8,9,10,10,10,10,11] * 4
    print("———————–")
    print("—–Black Jack——–")
    print("Would you take a card? [y]-yes [n] – no")
    count = 0
    comp_count = 0

    while True:
    select = input ("Your select: ")
    if select =="y":
    current_card = card_deck.pop()
    print(f"Your card is: {current_card}")
    count += current_card
    if count > 21:
    print("You lOSE!")
    print(f"Total score: {count}")
    break
    elif count == 21:
    print("You Win!")
    print(f"Total score : {count}")
    break
    else:
    print(f"Total score:{count}")
    elif select =="n":
    print(f"Stop.Total score: {count}")
    break

    print("————Game Over——–")
    for i in range(2):
    comp_count = random.choice("234567891010 10 10 11")

    print(f"Computer – {comp_count}")

  6. You are not using deck of cards and when drawn each card goes out of that deck, so you can draw same card all over again, dealer has 0 actions in this game, when you have blackjack there is possibility dealer also have one

  7. you should add a card deck like this it adds more realistic feel to the game

    from random import shuffle
    cardDeck=[]
    for j in ("Hearts", "Diamonds", "Spades", "Clubs"):
    for i in ("2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"):
    cardDeck.append("%s of %s"%(i, j))
    shuffle(cardDeck)
    print(cardDeck)

  8. This isn't actual blackjack. The dealer is supposed to keep hitting until at least 16, and even on 16 if the soft 16 rule applies.

  9. Thanks a bunch! There is one little logic error in that the dealer could have [ 11, 11 ] and he would actually win instead of bust. It's a great exercise to try to complete this so that the dealer plays on after the users stays. Anyway, thanks.

  10. It looks like the dealer will never have more than two cards?
    I think this code needs the dealer to draw cards when it has 16 or less points or something, if i'm not wrong.
    Thanks for the video!

LEAVE A REPLY

Please enter your comment!
Please enter your name here