Home > Net >  Updating variable in for loop
Updating variable in for loop

Time:11-03

Working on a homework assignment in which I have to write three functions that creates a simple game of blackjack.

  • First function card_to_value takes in a str that represents the card and the function returns the int value of each card with 'A' returning as only 11.
  • The second function calculates the hard score of a given hand in which 'A' is 11 regardless of the amount of aces in the hand.
  • The third function calculates the soft score in which the first 'A' in the hand is 11 but all subsequent aces are worth 1, so for example the hand 'AAA' should return 13 in the soft score function but 33 in the hard score function.

My code currently works for the first two functions but won't return the proper value for the soft score function.

def card_to_value(card=''):
    card_list=['2','3','4','5','6','7','8','9','T','J','Q','K','A']
        while card in list(card_list):
           if card in card_list[8:12]:
            card=10
            return card
           if card in card_list[12:]:
            card=1
            return card
           if card == '2':
            card=2
            return card
           if card == '3':
            card=3
            return card
           if card == '4':
            card=4
            return card
           if card == '5':
            card=5
            return card
           if card == '6':
            card=6
            return card
           if card == '7':
            card=7
            return card
           if card == '8':
            card=8
            return card
           if card == '9':
            card=9
            return card

def hard_score(hand):
    h = list(hand)
    total=0
    for each in h:
        total=total card_to_value(each)
    return total

def soft_score(hand):
    ace_found=False
    h = list(hand)
    total = 0
    for each in h:
        total = total   card_to_value(each)
        if each == 'A':
            ace_found=True
        elif ace_found == True:
            total  = 1
        else: total  = 11

return total

CodePudding user response:

You are adding to total twice. You need to check if it is an Ace before you call card_to_value.

def soft_score(hand):
    ace_found=False
    h = list(hand)
    total = 0
    for each in h:
        total = total   card_to_value(each)
        # handle the aces first 
        if each == 'A':
          if not ace_found :
            ace_found=True
            total  = 11
          else :
            total  = 1
        # handle all other cards
        else: 
            total  = card_to_value(each)

CodePudding user response:

Maybe something like this would be useful for you...

import itertools

# Dictionary of possible card values
card_values = {
    '2':[2],
    '3':[3],
    '4':[4],
    '5':[5],
    '6':[6],
    '7':[7],
    '8':[8],
    '9':[9],
    'T':[10],
    'J':[10],
    'Q':[10],
    'K':[10],
    'A':[11,1],
}

# Function that takes a list of cards considered a "hand"
# and returns all possible hand totals
def get_possible_totals(hand):
    values=[card_values.get(card) for card in hand]
    return [sum(tup) for tup in itertools.product(*values)]

Example usage:

hand = ['5','A','A']
get_possible_totals(hand)

returns:
[27, 17, 17, 7]

CodePudding user response:

I changed all the functions to show how it could be written. This might be too much input for a beginner the moment, but if you try and manage to understand it (in a few days or weeks or months) you've learned a lot.

The card_to_value function uses a dictionary to get the proper value for a card. Use print(card_values) in this function to see what the constructed dictionary looks like.

hard_score shows the usage of sum with a generator for the values.

soft_score uses the original approach. But since card_to_value now returns 11 for an ace we subtract 10 from the total for each ace after the first one.

def card_to_value(card=''):
    card_values = dict(zip('23456789TJQKA', [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]))
    return card_values.get(card, 0)


def hard_score(hand):
    return sum(card_to_value(card) for card in hand)


def soft_score(hand):
    ace_found = False
    total = 0
    for card in hand:
        total  = card_to_value(card)
        if card == 'A':
            if not ace_found:
                ace_found = True
            else:
                total -= 10  # subtract 10 if it is not the first ace
    return total


hand = ['3', 'A', '5', 'A']
print(hard_score(hand))
print(soft_score(hand))

This will print out 30 and 20.


Bonus version of soft_score:

def soft_score(hand):
    return hard_score(hand) - max(hand.count('A') - 1, 0) * 10

Idea: Count the number of aces in the hand, subtract 1, leave a minimum value of 0 , multiply this with 10 and subtract it from the hard score.

  • Related