Black Jack - determine a natural or black jack hand

Hello
I have just successfully completed the ‘Black Jack’ challenges.

By trying, I tried this code:

def is_blackjack(card_one, card_two):
    ten_list = ['10', 'K', 'Q', 'J']
    if card_one or card_two == 'A' and card_one or card_two in ten_list:
        return True
    else:
        return False 

This attempt failed, the test module said ’ 4, 7 != black jack’. I was a bit surprised. I immediately tried this code by myself and actually, it returns ‘True’ if 4 and 7 are the arguments. How ist this possible? Neither 4 nor 7 fulfill the criteria I set in the code.

TIA Firith

This is related to operator precendence. The and operator has higher precendence (evaluated first) than the or operator. So…

card_one or card_two == 'A' and card_one or card_two in ten_list
# is the same as
(card_one) or (card_two == 'A' and card_one) or (card_two in ten_list)

And (card_one) is always True when card_one is a non-empty string.

To demonstrate this a bit,

>>> (True or True) and (False)
False
>>> (True) or (True and False)
True

>>> True or True and False
True
2 Likes

Ah, thank you. Now it makes sense to me.

1 Like

The other place to ask this sort of question and get code feedback is by requesting mentoring on your code :blush: You get much more holistic feedback that way.