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

Function getHandValue

barebones/blackjack-barebones.py:83–104  ·  view source on GitHub ↗
(allCards)

Source from the content-addressed store, hash-verified

81
82
83def getHandValue(allCards):
84 value = 0
85 numberOfAces = 0
86
87 # Add the value for the non-ace cards:
88 for card in allCards:
89 rank = card[0] # card is a list like [rank, suit]
90 if rank == 'A':
91 numberOfAces += 1 # Aces are worth at least 1.
92 elif rank in ['K', 'Q', 'J']: # Face cards are worth 10.
93 value += 10
94 elif rank in ['2', '3', '4', '5', '6', '7', '8', '9', '10']:
95 value += int(rank) # Numbered cards are worth their number.
96
97 # Add the value for the aces:
98 value += numberOfAces # Add 1 per ace.
99 for i in range(numberOfAces):
100 # If another 10 can be added without busting, do so:
101 if value + 10 <= 21:
102 value += 10
103
104 return value
105
106
107def displayCards(allCards):

Callers 2

mainFunction · 0.70
displayHandsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected