DEV Community

Discussion on: Daily Challenge #128 - Blackjack Scorer

Collapse
 
edh_developer profile image
edh_developer

Python, with a couple assumptions:

  • ["A","10","A"] should return 12, not 22.
  • If the function is supposed to say "busted", it would have to do that before returning the corresponding numerical value.

def score(cards):
  facecards = ["K","Q","J"]
  total = 0
  aces = 0

  for c in cards:
    if c in facecards:
      total += 10
    elif c == "A":
      total += 11
      aces += 1
    else:
      total += int(c)


  for _ in range(aces):
    if total > 21:
      total -= 10

  if total > 21:
    print "Busted!"

  return total