Skip to content

Instantly share code, notes, and snippets.

@mjhea0
Last active February 5, 2025 14:32

Revisions

  1. mjhea0 revised this gist May 30, 2013. 1 changed file with 28 additions and 28 deletions.
    56 changes: 28 additions & 28 deletions python_blackjack.py
    Original file line number Diff line number Diff line change
    @@ -5,38 +5,38 @@

    def deal(deck):
    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 == 14:card = "A"
    hand.append(card)
    return 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 == 14:card = "A"
    hand.append(card)
    return hand

    def play_again():
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":
    dealer_hand = []
    player_hand = []
    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    game()
    else:
    print "Bye!"
    exit()
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":
    dealer_hand = []
    player_hand = []
    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    game()
    else:
    print "Bye!"
    exit()

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

    def hit(hand):
    card = deck.pop()
  2. mjhea0 revised this gist May 30, 2013. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions python_blackjack.py
    Original file line number Diff line number Diff line change
    @@ -5,26 +5,26 @@

    def deal(deck):
    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 == 14:card = "A"
    hand.append(card)
    return 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 == 14:card = "A"
    hand.append(card)
    return hand

    def play_again():
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":
    dealer_hand = []
    player_hand = []
    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    game()
    else:
    print "Bye!"
    exit()
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":
    dealer_hand = []
    player_hand = []
    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    game()
    else:
    print "Bye!"
    exit()

    def total(hand):
    total = 0
  3. mjhea0 revised this gist May 30, 2013. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions python_blackjack.py
    Original file line number Diff line number Diff line change
    @@ -16,15 +16,15 @@ def deal(deck):
    return hand

    def play_again():
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":
    dealer_hand = []
    player_hand = []
    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    game()
    else:
    print "Bye!"
    exit()
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":
    dealer_hand = []
    player_hand = []
    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    game()
    else:
    print "Bye!"
    exit()

    def total(hand):
    total = 0
  4. mjhea0 revised this gist May 30, 2013. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions python_blackjack.py
    Original file line number Diff line number Diff line change
    @@ -6,14 +6,14 @@
    def deal(deck):
    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 == 14:card = "A"
    hand.append(card)
    return hand
    random.shuffle(deck)
    card = deck.pop()
    if card == 11:card = "J"
    if card == 12:card = "Q"
    if card == 13:card = "K"
    if card == 14:card = "A"
    hand.append(card)
    return hand

    def play_again():
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
  5. mjhea0 created this gist May 30, 2013.
    119 changes: 119 additions & 0 deletions python_blackjack.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    import os
    import random

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

    def deal(deck):
    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 == 14:card = "A"
    hand.append(card)
    return hand

    def play_again():
    again = raw_input("Do you want to play again? (Y/N) : ").lower()
    if again == "y":
    dealer_hand = []
    player_hand = []
    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    game()
    else:
    print "Bye!"
    exit()

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

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

    def clear():
    if os.name == 'nt':
    os.system('CLS')
    if os.name == 'posix':
    os.system('clear')

    def print_results(dealer_hand, player_hand):
    clear()
    print "The dealer has a " + str(dealer_hand) + " for a total of " + str(total(dealer_hand))
    print "You have a " + str(player_hand) + " for a total of " + str(total(player_hand))

    def blackjack(dealer_hand, player_hand):
    if total(player_hand) == 21:
    print_results(dealer_hand, player_hand)
    print "Congratulations! You got a Blackjack!\n"
    play_again()
    elif total(dealer_hand) == 21:
    print_results(dealer_hand, player_hand)
    print "Sorry, you lose. The dealer got a blackjack.\n"
    play_again()

    def score(dealer_hand, player_hand):
    if total(player_hand) == 21:
    print_results(dealer_hand, player_hand)
    print "Congratulations! You got a Blackjack!\n"
    elif total(dealer_hand) == 21:
    print_results(dealer_hand, player_hand)
    print "Sorry, you lose. The dealer got a blackjack.\n"
    elif total(player_hand) > 21:
    print_results(dealer_hand, player_hand)
    print "Sorry. You busted. You lose.\n"
    elif total(dealer_hand) > 21:
    print_results(dealer_hand, player_hand)
    print "Dealer busts. You win!\n"
    elif total(player_hand) < total(dealer_hand):
    print_results(dealer_hand, player_hand)
    print "Sorry. Your score isn't higher than the dealer. You lose.\n"
    elif total(player_hand) > total(dealer_hand):
    print_results(dealer_hand, player_hand)
    print "Congratulations. Your score is higher than the dealer. You win\n"

    def game():
    choice = 0
    clear()
    print "WELCOME TO BLACKJACK!\n"
    dealer_hand = deal(deck)
    player_hand = deal(deck)
    while choice != "q":
    print "The dealer is showing a " + str(dealer_hand[0])
    print "You have a " + str(player_hand) + " for a total of " + str(total(player_hand))
    blackjack(dealer_hand, player_hand)
    choice = raw_input("Do you want to [H]it, [S]tand, or [Q]uit: ").lower()
    clear()
    if choice == "h":
    hit(player_hand)
    while total(dealer_hand) < 17:
    hit(dealer_hand)
    score(dealer_hand, player_hand)
    play_again()
    elif choice == "s":
    while total(dealer_hand) < 17:
    hit(dealer_hand)
    score(dealer_hand, player_hand)
    play_again()
    elif choice == "q":
    print "Bye!"
    exit()

    if __name__ == "__main__":
    game()