Python > Black jack > Task 3: Value of an ace

Hello stars!

Here is my context:

  • The function value_of_card(card) works fine;
  • I am stuck with the function value_of_ace(card_one, card_two) which I am copyting/pasting below;
  • I am getting 2 types of errors:
    First type of error: ‘’‘AssertionError: None != 1 : Called value_of_ace(Q, A). The function returned None, but the test expected 1 as the value of an ace card when the hand includes (‘Q’, ‘A’).’‘’
    Second type of error: ```AssertionError: 11 != 1 : Called value_of_ace(2, A). The function returned 11, but the test expected 1 as the value of an ace card when the hand includes (‘2’, ‘A’).````

Question: Can you help me finishing this exercise?

def value_of_ace(card_one, card_two):
    """Calculate the most advantageous value for the ace card.
    :param card_one, card_two: str - card dealt. See below for values.
    :return: int - either 1 or 11 value of the upcoming ace card.
    1.  'J', 'Q', or 'K' (otherwise known as "face cards") = 10
    2.  'A' (ace card) = 11 (if already in hand)
    3.  '2' - '10' = numerical value.
    """
    value_in_hand = value_of_card(card_one) + value_of_card(card_two)
    option_1 = value_in_hand + 1
    option_11 = value_in_hand + 11

    if option_1 <= 21 and option_11 <=21:
        # Compare both options
        if (21 - option_1) < (21 - option_11):
            return 1
        else:
            return 11

I have tweaked my code to try covering all possible scenarios:

def value_of_ace(card_one, card_two):
    """Calculate the most advantageous value for the ace card.
    :param card_one, card_two: str - card dealt. See below for values.
    :return: int - either 1 or 11 value of the upcoming ace card.
    1.  'J', 'Q', or 'K' (otherwise known as "face cards") = 10
    2.  'A' (ace card) = 11 (if already in hand)
    3.  '2' - '10' = numerical value.
    """
    value_in_hand = value_of_card(card_one) + value_of_card(card_two)
    option_1 = value_in_hand + 1
    option_11 = value_in_hand + 11

    if (option_11) > 21 and (value_in_hand + 1) > 21:
        return 0
    elif (option_11) > 21:
        return 1
    elif (option_1) > 21:
        return 11 
    else:
        # Compare both options
        if (21 - option_1) < (21 - option_11):
            return 1
        else:
            return 11

But I am still getting the following two errors:

Error #1:

AssertionError: 11 != 1 : Called value_of_ace(2, A). The function returned 11, but the test expected 1 as the value of an ace card when the hand includes ('2', 'A').

Error #2:

AssertionError: 11 != 1 : Called value_of_ace(A, 2). The function returned 11, but the test expected 1 as the value of an ace card when the hand includes ('A', '2').
  1. Do you understand what the test is doing?
  2. Do you understand what the test is expecting and why?
  3. Do you understand what your code returns and how it differs?
  4. Do you understand why your code is returning what it returns?

I was a little confused about this particular function.
I think the confusion is one not being familiar with blackjack and what they mean by a hand.

I originally thought if you return 11 or 1 you’d change the hand and then I saw the comment
‘A’ (ace card) = 11 (if already in hand) but value_of_card would return 1.
I was confused about how to handle this. But I was like essentially no matter what it should be a sum less than 21. To cover my bases I was like card_one + card_two + 11 <= 21 return 11 or if one of the card is A then test if it’s under 21 whether it’s 1 or 11. That gave me the same error as above.

So I decided to understand the test better(reverse engineering ;))

test_data = [('2', '3', 11), ('3', '6', 11), ('5', '2', 11),
                     ('8', '2', 11), ('5', '5', 11), ('Q', 'A', 1),
                     ('10', '2', 1), ('7', '8', 1), ('J', '9', 1),
                     ('K', 'K', 1), ('2', 'A', 1), ('A', '2', 1)]

So then I realize the sum of the hand looked like it should always be under 11 and that then if we did the sum of the given hand is less than 11 + 11 should be less than 21(That worked). So this is essentially what I’m assuming & my thought pattern. Hopes that helped :slight_smile:

1 Like

Given that it’s been 5 months, I suspect they’re no longer working on this exercise :joy:

But maybe that another rookie might find interesting the other answers.