Search code examples
pythonpython-3.xblackjack

Standing choice not doing anything


So I am trying to make a simple blackjack game in python 3 and everything works just fine, except that I can't stand. If I input 2 it won't do anything. Thanks in advance. EDIT 1: Hitting works well. EDIT 2: Posted the entire script, so you can reproduce the problem I'm facing, as @roganjosh suggested.

from random import shuffle
import sys


def deal(deck, player, dealer):
    shuffle(deck)

    for _ in range(2):
        player.append(deck.pop())
        dealer.append(deck.pop())

def score(hand):
    non_aces = [c for c in hand if c != 'A']
    aces = [c for c in hand if c == 'A']

    sum = 0

    for card in non_aces:
        if card in 'JQK':
            sum += 10
        else:
            sum += int(card)

    for card in aces:
        if sum <= 10:
            sum += 11
        else:
            sum += 1

    return sum

def display_info(player, dealer, stand):
    print("Your cards: [{}] ({})".format(']['.join(player), score(player)))
    if stand:
        print("Dealer cards: [{}] ({})".format(']['.join(dealer), score(dealer)))
    else:
        print("Dealer cards: [{}] [?]".format(dealer[0]))

def results(player, dealer, hand, stand):
    if score(player) == 21 and hand:
        print("Blackjack! You won!")    
        sys.exit()    
    elif score(player) > 21:
        print("Busted. You lost!")   
        sys.exit()     
    if stand:
        if score(dealer) > 21:
            print("Dealer busted. You won!")
        elif score(player) > score(dealer):
            print("You beat the dealer! You won!")
        elif score(player) < score(dealer):
            print("You lost!")
        else:
            print("Push. Nobody wins or losses.")
        sys.exit()

def hit_stand(deck, player, dealer, hand, stand):
    print("What would you like to do")
    print("[1] - Hit\n[2] - Stand")
    choice = input("> ")
    hand = False
    if choice == '1':
        player.append(deck.pop())
    elif choice == '2':
        stand = True
        while score(dealer) <= 16:
            dealer.append(deck.pop())

if __name__ == '__main__':
    deck = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']*4
    player = []
    dealer = []
    standing = False
    first_hand = True
    deal(deck, player, dealer)
    while True:
        display_info(player, dealer, standing)
        results(player, dealer, first_hand, standing)
        hit_stand(deck, player, dealer, first_hand, standing)

Solution

  • You are not checking the result after the player chooses to stand. Since you only deal() once before the while True loop, you just get an infinite condition if you choose to stand repeatedly. Calculate the score after the dealer has drawn all their cards.

    def hit_stand(deck, player, dealer, hand, stand):
        print("What would you like to do")
        print("[1] - Hit\n[2] - Stand")
        choice = input("> ")
        hand = False
        if choice == '1':
            player.append(deck.pop())
        elif choice == '2':
            stand = True
            while score(dealer) <= 16:
                print(score(dealer))
                dealer.append(deck.pop())
            display_info(player, dealer, stand)
            results(player, dealer, first_hand, stand) # HERE
    

    On a somewhat unrelated note, crashing out of the game after the final score is determined is not very elegant. You will want to look at a better construct than while True: and sys.exit() to control flow, but that's an exercise for you.

    Finally, you should not be using sum as a variable name inside score() because this is a built-in function that you're redefining. Use something like total so that you don't risk masking the built-in function itself.