#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0

    Errors with my program


    I have been trying to write a blackjack program that shuffles the cards 100 times (the "suits" does not matter); when you call the Hit function it will get three cards and choose the biggest maximum of the three cards; also, the dealer move" can get new cards until the deal score is more than 15 but less than 19. I believe I have done that, however, I get errors like indenting issues and string comparison issues. How can I address all of these issues? May you guys help me with these errors? Thanks!

    Code:
    import random
    import math
    
       
    def generateDeck(): 
    	cards = [10,9,8,7,6,5,4,3,2]
    	cards.append('A')
    	cards.append('K')
    	cards.append('Q')
    	cards.append('J')
    
    	cards  = cards*4
    	for i in range(100):
                    final=random.shuffle(cards)
    	return final
    
    
    def values(cards):
    	totalvalue = 0
    	for c in cards:
    		if c=='J' or c=='Q' or c=='K':
    			value = 10
    		elif c=='A':
    			value = 1
    		else:
    			value = c
    
    		totalvalue = totalvalue + value
    		
    	if 'A' in cards and totalvalue <= 11:
    		totalvalue = totalvalue + 10
    	
    	return totalvalue
    
    
    def Hit(deck, hand):
    	drawcard1 = deck.pop() #remove 1 card from deck
    	drawcard2 = deck.pop()
    	drawcard3 = deck.pop()
    	final = max(eval(drawcard1, drawcard2, drawcard3))
    	hand.append(final)
    	return final
    
    
    def dealCards(deck, hand): 
    	Hit(deck, hand)
    	Hit(deck, hand)
    
    
    def playermove(deck, playerHand):
    	answer = input("Would you like to hit or stay? (h/s) ") 
    	answer = answer.lower()
    	while answer != 's':
    		drawCard = Hit(deck, playerHand)
    		print ("player draw ", drawCard)								   
    		
    		playerPoint = values(playerHand)
    		print ("player:", playerHand, '==>', playerPoint)				
    		
    		if playerPoint >= 21:
    			break
    		else:
    			answer = input("Would you like to hit or stay? (h/s) ")
    			answer = answer.lower()
    
    
    def dealermove(deck, dealerHand):
    	dealerPoint = values(dealerHand)
    	while (dealerPoint >= 15 or dealerPoint < 19):
    		drawCard = Hit(deck, dealerHand)
    		dealerPoint = values(dealerHand)
    		
    		print ("dealer draw ", drawCard)
    		print ("Dealer:", dealerHand, '==>', dealerPoint)
    
    
    
    def winner(dealerHand, playerHand):
    	playerPoint = values(playerHand)
    	dealerPoint = values(dealerHand)
    	if playerPoint > dealerPoint:
    		print ("You win!")
    	elif dealerPoint > playerPoint:
    		print ("You lose!")
    	elif playerPoint == dealerPoint:
    		print ("Tie Game!")
    
    
    def blackjack_game():
    	deck = generateDeck()
    	playerHand = [ ]
    	dealerHand = [ ]
    
    	#step 1
    	dealCards(deck, playerHand)
    	dealCards(deck, dealerHand)
    	
    	playerPoint = values(playerHand)
    	dealerPoint = values(dealerHand)
    
    	print ("player: ", playerHand, '==>', playerPoint)
    	print ("dealer: ", dealerHand, '==>', dealerPoint)
    
    	if playerPoint == 21 or dealerPoint == 21:
    		if dealerPoint == playerPoint:
    			print ("Tie Game!")
    		elif dealerPoint == 21:
    			print ("You lose!")
    		else:
    			print ("You win!")
    		return
    	
    	# step 2
    	playermove(deck, playerHand)
    	playerPoint = values(playerHand)
    	if playerPoint > 21:
    		print ("You lose!")
    		return
    	
    	# step 3
    	dealermove(deck, dealerHand)
    	dealerPoint = values(dealerHand)
    	if dealerPoint > 21:
    		print ("You win!")
    		return
    	
    	# step 4
    	winner(dealerHand, playerHand)
    	
    
    def main():
    	ans = ''
    	while ans != 'n':
    		print ("\n***********")
    		print ("  BJ Game")
    		print ("***********")
    	
    		blackjack_game()
    		ans = input("Would you like to play again ? (y/n) ")
    		ans = ans.lower()
    	
    	print ("Goodbye")
    
    
    main()
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    615
    Rep Power
    65
    Random shuffles cards in place. Since it's random one shuffle enough.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    514
    Rep Power
    33
    Indentation issues usually mean that you are mixing tabs and spaces. Use spaces only, as every web site or individual can define a tab to be anything they want.

    Not too may people will respond to "fix my program" for ~150 lines of code that has not been tested. Test each function individually. For example, generateDeck() returns final which is None because it is the return from random.shuffle(). A simple print statement would show that you are not getting the cards list.

    Also, in Hit() the statement
    final = max(eval(drawcard1, drawcard2, drawcard3))
    does not make sense and would probably result in an error if tested. There is no formula to eval and max would yield an ASCII compare, so K, Q, and J would all be higher than A. I would think that you want to return an Ace first if one was drawn, then a face card, and finally the highest of the numbered cards.
    Code:
    def generateDeck():.
        cards = [10,9,8,7,6,5,4,3,2]
        cards.append('A')
        cards.append('K')
        cards.append('Q')
        cards.append('J')
    
        cards  = cards*4
        for i in range(100):
            random.shuffle(cards)
        return cards
    
    
    def Hit(deck, hand):
        cards_drawn = [deck.pop() for ctr in range(3)]
        print cards_drawn
        for card in cards_drawn:
            if card == "A":
                hand.append(card)
                return card
        for card in cards_drawn:
            if card in ("K", "Q", "J"):
                hand.append(card)
                return card
    
        ## all cards drawn were in the 2-10 range
        final = max(cards_drawn)
        hand.append(final)
        return final
    
    deck = generateDeck()
    hand = []
    print Hit(deck, hand)
    print hand
    print Hit(deck, hand)
    print hand
    Last edited by dwblas; December 7th, 2013 at 06:03 PM.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    Isn't the skill of blackjack in taking the
    MIDDLE maximum of the three cards?
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    I'm getting this output:
    Cards Drawn: [6, 5, 'Q']
    Cards Drawn: [4, 2, 8]
    Cards Drawn: [4, 6, 3]
    Cards Drawn: [10, 'K', 7]
    player: ['Q', 8] ==> 18
    dealer: [6, 'K'] ==> 16
    Would you like to hit or stay? (h/s) h
    Cards Drawn: [8, 9, 3]
    player draw 9
    player: ['Q', 8, 9] ==> 27
    You lose!
    Would you like to play again ? (y/n)

    I am trying to show the Cards Drawn from either the player or dealer, however, it is not turning out great. How should I fix that?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    I suggest you define exactly how the game play should look. What you've shown so far makes little sense to me.

    Let's suppose dealing from the deck returns:

    6, 5, 'Q', 4, 2, 8, 4, 6, 3, 10, 'K', 7, 8, 9, 3




    Please understand. You've said "This is my output. It's wrong." What would be correct???
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo