"""Functions to help play and score a game of blackjack.
How to play blackjack: https://bicyclecards.com/how-to-play/blackjack/
"Standard" playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
"""
def value_of_card(card):
"""Determine the scoring value of a card.
:param card: str - given card.
:return: int - value of a given card. See below for values.
1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10
2. 'A' (ace card) = 1
3. '2' - '10' = numerical value.
"""
match card:
case 'A':
return 1
case 'J' | 'Q' | 'K':
return 10
case 2 | 3 | 4 | 5 | 6 | 7 |8 | 9 | 10:
return int(card)
def higher_card(card_one, card_two):
"""Determine which card has a higher value in the hand.
:param card_one, card_two: str - cards dealt in hand. See below for values.
:return: str or tuple - resulting Tuple contains both cards if they are of equal value.
1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10
2. 'A' (ace card) = 1
3. '2' - '10' = numerical value.
"""
if card_one == 'J' or 'Q' or 'K':
card_one = 10
elif card_two == 'J' or 'Q' or 'K':
card_two = 10
elif card_one == 'A':
card_one = 1
elif card_two == 'A':
card_two = 1
else:
card_one = int(card_one)
card_two = int(card_two)
card_one_is_higher = card_one > card_two
card_two_is_higher = card_two > card_one
cards_are_equal = card_one == card_two
if card_one_is_higher:
return int(card_one)
if card_two_is_higher:
return int(card_two)
if cards_are_equal:
return int(card_one), int(card_two)
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.
"""
if card_one == 'J' or 'Q' or 'K':
card_one = 10
elif card_two == 'J' or 'Q' or 'K':
card_two = 10
elif card_one == 'A':
card_one = 11
elif card_two == 'A':
card_two = 11
else:
card_one = int(card_one)
card_two = int(card_two)
sum_of_cards_over_21 = card_one + card_two + 11 > 21
sum_of_cards_less_or_equal_21 = card_one + card_two + 11 <= 21
if sum_of_cards_over_21:
return 1
if sum_of_cards_less_or_equal_21:
return 11
Hi! Currently doing the black jack exercise. I would like to understand why my code is not working at that point.
Errors are the following:
FAILED black_jack_test.py::BlackJackTest::test_higher_card - TypeError: '>' not supported between instances of 'int' and 'str'
FAILED black_jack_test.py::BlackJackTest::test_value_of_ace - TypeError: unsupported operand type(s) for +: 'int' and 'str'
FAILED black_jack_test.py::BlackJackTest::test_value_of_card - AssertionError: None != 2 : Expected 2 as the value of 2.
You got that error because you copied my code, which don’t have all functions written.
Actual errors are this:
FAILED black_jack_test.py::BlackJackTest::test_higher_card - TypeError: '>' not supported between instances of 'int' and 'str'
FAILED black_jack_test.py::BlackJackTest::test_value_of_ace - TypeError: unsupported operand type(s) for +: 'int' and 'str'
FAILED black_jack_test.py::BlackJackTest::test_value_of_card - AssertionError: None != 2 : Expected 2 as the value of 2.
This is why it’s helpful to share the entire code and entire error
FAILED black_jack_test.py::BlackJackTest::test_higher_card - TypeError: '>' not supported between instances of 'int' and 'str'
The test_higher_card code leads to comparing an int and str.
Looking at your higher_card function, is it possible that, in some combination of inputs, card_one_is_higher = card_one > card_two is testing an int and a str? Can card_one be an int while card_two is a str?
FAILED black_jack_test.py::BlackJackTest::test_value_of_ace - TypeError: unsupported operand type(s) for +: 'int' and 'str'
Similarly in value_of_ace, can card_one be an int and card_two be a string when you get to sum_of_cards_over_21 = card_one + card_two + 11 > 21?
FAILED black_jack_test.py::BlackJackTest::test_value_of_card - AssertionError: None != 2 : Expected 2 as the value of 2.
Is it possible that value_of_ace returns None here (probably implicitly, i.e. by getting to the end of the function without executing an explicit return statement)?