Blackjack exercise. Why is my code not working?

"""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.

How did you determine that it “is not working”? Is there an error, or a test failure, or …?

Yes, test error in all functions. Drops None instead of value.

When requesting help, it’s helpful to include the exact error messages.

» pytest
============================================== test session starts ==============================================
platform linux -- Python 3.10.9, pytest-7.2.1, pluggy-1.0.0
rootdir: /home/goodi/play/codes/mygithub/exercism/python, configfile: pytest.ini
plugins: cov-4.0.0, asyncio-0.20.3, timeout-2.1.0, forked-1.6.0, xdist-3.2.0, localserver-0.0.0, mock-3.10.0, sopel-7.1.9, lazy-fixture-0.6.3
asyncio: mode=strict
collected 0 items / 1 error

==================================================== ERRORS =====================================================
________________________________ ERROR collecting black-jack/black_jack_test.py _________________________________
ImportError while importing test module '/home/goodi/play/codes/mygithub/exercism/python/black-jack/black_jack_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
black_jack_test.py:4: in <module>
    from black_jack import (
E   ImportError: cannot import name 'is_blackjack' from 'black_jack' (/home/goodi/play/codes/mygithub/exercism/python/black-jack/black_jack.py)
============================================ short test summary info ============================================
ERROR black_jack_test.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================== 1 error in 0.26s ================================================
11:01 nuc ~/exercism/python/black-jack »

The error I’m seeing it ImportError: cannot import name 'is_blackjack' from 'black_jack'. Did you drop a needed function definition?

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 :slight_smile:

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)?

Thanks for the feedback. I refactored my code into a list + if/else statements and now is working, mostly.