The value of ACE in Black Jack

Mike C. Fletcher mcfletch at rogers.com
Mon Feb 17 16:40:14 EST 2003


Yes, a function is probably what you want:

def value( card, currentTotal=0 ):
    """Determine the value of an Ace for a hand with given total
    """
    if card != ACE:
        return card + currentTotal
    if currentTotal > 10:
        return 1 + currentTotal
    return 11 + currentTotal

Note that functions can use globals which are defined after the function 
definition.  Also note that using input is generally a bad idea, though 
likely not a concern for you at the moment.  You can now do something like:

    sum = value( card, sum )

You'd also likely find a function like:

def getCard( deck ):
    index = random.randint( 0,len(deck)-1)
    card = deck.pop(index)
    return card

would eliminate the situation where you deal 5 2's and get accused of 
fixing the deck :) .  Of course, that means you need your deck to have 
all 52 cards, but that can be done in your code just by multiplying 
"cards" by four.

Enjoy yourself,
Mike


Ole Jensen wrote:

>In Black Jack the value of the ACE depends on the sum of your cards if the
>sum is lower than 21, the value of your ACE is 11
>however if the sum of your cards increase to more than 21(if ace counts for
>11) then your ACE drops to the value of one.
>
>How would you define the value of ACE in python to the rules of Black Jack?
>
>Im sure the best way is to make some sort of a function but as a nooB, im
>not really sure how to get that to work (i might be able to make the
>function but Im not sure how to integrate it with the rest of the code)...
>
>  
>
...

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list