MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / getHandValue

Function getHandValue

src/gamesbyexample/blackjack.py:169–192  ·  view source on GitHub ↗

Returns the value of the cards. Face cards are worth 10, aces are worth 11 or 1 (this function picks the most suitable ace value).

(cards)

Source from the content-addressed store, hash-verified

167
168
169def getHandValue(cards):
170 """Returns the value of the cards. Face cards are worth 10, aces are
171 worth 11 or 1 (this function picks the most suitable ace value)."""
172 value = 0
173 numberOfAces = 0
174
175 # Add the value for the non-ace cards:
176 for card in cards:
177 rank = card[0] # card is a tuple like (rank, suit)
178 if rank == 'A':
179 numberOfAces += 1
180 elif rank in ('K', 'Q', 'J'): # Face cards are worth 10 points.
181 value += 10
182 else:
183 value += int(rank) # Numbered cards are worth their number.
184
185 # Add the value for the aces:
186 value += numberOfAces # Add 1 per ace.
187 for i in range(numberOfAces):
188 # If another 10 can be added without busting, do so:
189 if value + 10 <= 21:
190 value += 10
191
192 return value
193
194
195def displayCards(cards):

Callers 2

mainFunction · 0.70
displayHandsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected