As mentioned before, an ace can be worth either 1 or 11 points. Players try to get as close as possible to a score of 21, without going over 21 (going “bust”).
Define the value_of_ace(<card_one>, <card_two>)
function with parameters card_one
and card_two
, which are a pair of cards already in the hand before getting an ace card. Your function will have to decide if the upcoming ace will get a value of 1 or a value of 11, and return that value. Remember: the value of the hand with the ace needs to be as high as possible without going over 21.
Hint: if we already have an ace in hand then its value would be 11.
>>> value_of_ace('6', 'K')
1
>>> value_of_ace('7', '3')
11
def value_of_ace(card_one, card_two):
if 'A' in (card_one, card_two):
if value_of_card(card_one) == 10:
return 1
elif value_of_card(card_two) == 10:
return 1
elif value_of_card(card_one) == 2:
return 1
elif value_of_card(card_two) == 2:
return 1
else:
return 11
else:
total_value = value_of_card(card_one) + value_of_card(card_two)
if total_value <= 10:
return 11
else:
return 1
before adding the check for 2, i was having the 11th test case not working:
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’).
I’m not familiar with the game but from what I understood 2+11 =13 which is closer to 21 compared to 2+1 = 3