i already have a "def show hand" section, which shows winning hands. but i need it to do wht i have written below

here is a black jack code, i want it basicialyy show score for each game detailing the users total, the computers total and the winner
and When the user quits the program, show a list of all hands played, the user and computer totals and the number of hands played and who won how many hands i.e.:

4 hands played
You win by 3 hands to 1
Hand 1: user 19 computer 18
Hand 2: user 20 computer bust
Hand 3: user 16 computer bust
Hand 4: user 18 computer 18

below is the code :

import random as r
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]*4
computer = []
player = []
c = 'y'

#Clear works only if you run it from command line
def clear():
    import os
    if os.name == 'nt':
        os.system('CLS') #Pass CLS to cmd
    if os.name == 'posix':
        os.system('clear') #Pass clear to terminal
[B]def showHand():
    hand = 0
    for i in player: hand += i #Tally up the total
    print "The computer is showing a %d" % computer[0]
    print "Your hand totals: %d (%s)" % (hand, player)[/B]

#Gives player and computer their cards
def setup():
    for i in range(2):
        dealcomputer = deck[r.randint(1, len(deck)-1)]
        dealPlayer = deck[r.randint(1, len(deck)-1)]
while c != 'q':
    c = raw_input("[D]ealt [S]tick [Q]uit: ").lower()
    if c == 'd':
        dealPlayer = deck[r.randint(1, len(deck)-1)]
        hand = 0
        for i in computer: hand += i
        if not hand > 17:   #computer strategy.
            dealplayer = deck[r.randint(1, len(deck)-1)]
        hand = 0
        for i in player: hand += i
        if hand > 21:
            print "BUST!"
            player = []     #Clear player hand
            computer = []     #Clear computer's hand
            setup()         #Run the setup again
        hand = 0
        for i in computer: hand +=i
        if hand > 21:
            print "computer Busts!"
            player = []
            computer = []
    elif c == 's':
        cHand = 0           #computer's hand total
        pHand = 0           #Player's hand total
        for i in computer: cHand += i
        for i in player: pHand += i
        if pHand > cHand:
            print "you Win!"    #If playerHand (pHand) is greater than computeHand (cHand) you win...
            computer = []
            player = []
            print "computer win!"    #...otherwise you loose.
            computer= []
            player = []
        if c == 'q':

            gb= raw_input("Press Enter (q to quit): ").lower()
            if 'q' in exit:

       # print "Wins, player = %d  computer = %d" % (player, computer)

    print "Thanks for playing blackjack with the computer!"
8 Years
Discussion Span
Last Post by Murtan

Ok this first comment is pretty minor, but I'll mention it anyway. The code for setup randomly selects the players card and the dealers card, then removes both cards from the deck. It does this twice to for the initial two cards. There is a chance (pretty small, but it is still there) that the dealer and the player could select the same random index from the deck. You would then add the same card to both hands and then remove the same index twice. The second call would remove a card that wasn't in either hand. You might consider a 'deal a card' function that would generate a random index into the deck, save the card it found, remove the card from the deck and then return the 'saved' card. Alternatively, you could pass the hand that it should append the card to if you like that better.

More importantly, the logic inside the loop isn't quite right. The blackjack game normally allows the player to draw more than once if they want to, so you can't compare the hands until the player 'stays'.

Also, the dealer doesn't get to take any cards until the player 'stays', but always has that option when the player does 'stay'.

You will need to maintain a list of the hand results if you want to be able to 'recap' the hands when they quit. It could be as simple as appending a tuple with the player score and then computer score (or 'bust' as appropriate) to a list. Then when the game is over you iterate the list outputting the hand number, player score and computer score. The tracking for total hands played and number of hands won should probably be maintained in counter variables, it doesn't make much sense to have to iterate the results list to figure it out again at the end.

if the list were 'handresults' it could be declared and filled like this:

handresults = []

Ok, after looking at that I have one more comment and another idea. The comment is that when the scores are tied, it is a draw, no-one wins.

The other idea would be to just put the output line in the list:

handresults = []
handresults.append("Hand 1: user 19 computer 18")
# add remaining hands here

thanks for the help, but were do i add the code, in the the code i have. would it be below the "def show hand" part of the code.. if you dont no were the "def show hand" code is , if you see my previous posts.


If you make the new code as a function (def ...), you can just add it anywhere near the other def statements (before you start the game). If not you have to look at your main game loop again and see where to insert the code. I think it is easier to create separate def's because you can test them before you add them to your main game.

hope this helps


I think it was pretty obvious that I saw and read your code.

I don't believe in solving the problem for you, I believe in giving you the tools to help yourself.

One of the really cool things about python is that it is pretty fast to try most things, and if you're not sure how something works, you can play with it interactively to see how it really works and functions.

If you're not sure where to put stuff, make an educated guess as to where it would go and then try it. If it didn't work, but gave you another idea try that...if you've been struggling for a bit and have run out of ideas, post what you have (or the part you're having trouble with) and I'll try to help some more.

For example the part about saving the results...you probably have to declare the empty list before you start playing and add something to the list every time you determine a winner (or a tie). Then at the very end of the program, you go back through the list to recap the results.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.