0

I wrote this function to "score" hands for a Python blackjack game (I know aces can be 1 or 11, that's a future problem to figure out). The input is a list of tuples (cards), and the rank can be either 2-10 or A, J, K, or Q.

I'm sometimes getting a TypeError that reads TypeError: unsupported operand type(s) for +: 'int' and 'str' in reference to score = score + rank. I'm not sure why there's an issue though, because if it gets to that point the rank must be an int. Anyone see the issue?

def analyzeHand(hand):
    score = 0
    for card in hand:
        rank = card[0]
        if (rank == 'A'):
            score = score + 11
        if (rank == 'K' or rank == 'Q' or rank == 'J'):
            score = score + 10
        else:
            score = score + rank
    return score

1 Answers1

1

That score = score + rank in the last else should likely be

score += int(rank)

because from context rank is of type str, assumed to be "2".."9".

user2864740
  • 60,010
  • 15
  • 145
  • 220
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218