0

writing code for a blackjack game. Built an 'if' statement that evaluates the first character of the string that spells out the card, so I can assign an 'int' value to the card. Ex. the '[0]' index should be 'k' for king and the value should increase by 10.

    #face cards
    if card_1_split[0] == 'k' or 'q' or 'j':
        card_value += 10
        
    elif card_1_split[0] == 'a':
        #determining ace
        ace_value = input('Do you want the Ace to be an 11 or 1?>>')
        if ace_value == '11':
            card_value += 11
        else:
            card_value +=1
            
    #number cards        
    else:
        card_value += int(card_1_split[0])

but for some reason the first if statement always returns true (even when it is not) and the card value increases by 10.

here is the output for two cards randomly being drawn from the deck

''' 2_of_hearts 8_of_clubs

2 #I printed out the index[0] for the first card to show that the indexing is correct.

Your total points are: 20 '''

the card value should increase by 2 then 8, but it always increases by 10. the total is twenty because i ran the first block of code above twice (one for each card), but only included the code for the first card because they are the same thing.

NotAName
  • 3,821
  • 2
  • 29
  • 44
gavmross
  • 21
  • 4
  • the `or` statement works by short circuiting, similar to `||` in java. so in java you would be saying in the if stmnt above: `if x == 'k' || 'q' (is truthy) || 'j' (is truthy)`, which always evaluates to true, because the value `q` is truthy. – rv.kvetch Oct 11 '21 at 00:57
  • in this case you probably want if card in ('k', 'q', 'j'): – Kenny Ostrom Oct 11 '21 at 00:59
  • Look at [Operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence). `==` has higher precedence than `or` so `card_1_split[0] == 'k'` is resolved first and its result becomes the first value for the two `or` tests`. – tdelaney Oct 11 '21 at 01:15

2 Answers2

0

One way of looking at this is 'expanding' how python treats 'truthy' values. Non-empty strings count as true, empty strings count as false, so what you're writing here is:

if card_1_split[0] == 'k' or true or true:

So it tests for:

  • Is card_1_split[0] == 'k'? (Maybe)
  • Is the boolean representation of 'q' true? (Yes, always)
  • Is the boolean representation of 'j' true? (Yes, always)

Both of the latter two are true, so you always get true.

You have two options:

if card_1_split[0] == 'k' or card_1_split[0] == 'q' or card_1_split[0] == 'j':

Or, perhaps more elegantly:

if card_1_split[0] in ['k', 'q', 'j']:
Alex
  • 2,270
  • 3
  • 33
  • 65
0

Python evaluate the boolean card_1_split[0] == 'k' or 'q' or 'j' as (card_1_split[0] == 'k') or 'q' or 'j'. The strings 'q'and'j'` always evaluate to true. Only certain values in python evaluate to False and all others evaluate to true by default.

https://www.programiz.com/python-programming/methods/built-in/bool

Andrew-Harelson
  • 1,022
  • 3
  • 11