Question: I have a BlackJack Python code. I have a question on what the end values are supposed to be for your cards. When delt {8,2}

I have a BlackJack Python code. I have a question on what the end values are supposed to be for your cards. When delt {8,2} the program says the value of the two added together is 24... Where did I go wrong?

` import os import random

deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4

def deal(deck): # deck function hand = [] for i in range(2): random.shuffle(deck) # shuffles deck card = deck.pop() # removes and returns the value back to card if card == 11:card = "J" # sets 11 to jack if card == 12:card = "Q" # sets 12 to queen if card == 13:card = "K" # sets 13 to king if card == 14:card = "A" # sets 14 to ace hand.append(card) return hand

def play_again(): # ask payer if wants to play again funciton again = raw_input("Do you want to play again? (Y/N) : ").lower() if again == "y": # if answers yes, will give the player more cards dealer_hand = [] player_hand = [] deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4 #sets deck to values of cards game() else: print ("Bye!") # exits program exit()

def total(hand): total = 0 # start off with total of zero for card in hand: # for the cards you rdelt if card == "J" or card == "Q" or card == "K": # if the card is j,q,k it equals 10 total += 10 elif card == "A": if total >= 11: total+= 1 # if the card is an A, if the total is more than 11, it makes the vaue 1 else: total += 11 # makes the value of the ace 1 if the total is greater than 11 else: total += card # adds up your cards return total

def hit(hand): card = deck.pop() # removes and returns the value back to card if card == 11:card = "J" # setting the card number to the face value if card == 12:card = "Q" if card == 13:card = "K" if card == 14:card = "A" hand.append(card) return hand

def clear(): if os.name == 'nt': os.system('CLS') if os.name == 'posix': os.system('clear')

def print_results(dealer_hand, player_hand): # funtcion for results of hand clear() print ("The dealer has a " + str(dealer_hand) + " for a total of " + str(total(dealer_hand))) # gives you dealers hand print ("You have a " + str(player_hand) + " for a total of " + str(total(player_hand))) # gives you your hand

def blackjack(dealer_hand, player_hand): if total(player_hand) == 21: print_results(dealer_hand, player_hand) # prints dealers and your hand print ("Congratulations! You got a Blackjack! ") play_again() elif total(dealer_hand) == 21: # if hand is more than 21, print_results(dealer_hand, player_hand) # prints out dealer and your hand print ("Sorry, you lose. The dealer got a blackjack. ") play_again()

def score(dealer_hand, player_hand): if total(player_hand) == 21: # if your hand = 21 print_results(dealer_hand, player_hand) # shows dealers/ your hand print ("Congratulations! You got a Blackjack! ") elif total(dealer_hand) == 21: # if dealer hand = 21 print_results(dealer_hand, player_hand) # shows dealers/ your hand print ("Sorry, you lose. The dealer got a blackjack. ") elif total(player_hand) > 21: # if your hand is greater than print_results(dealer_hand, player_hand) # shows dealers/ your hand print ("Sorry. You busted. You lose. ") elif total(dealer_hand) > 21: # if dealer hand is greater than 21 print_results(dealer_hand, player_hand) # shows dealers/ your hand print ("Dealer busts. You win! ") elif total(player_hand) < total(dealer_hand): # if your hand is less than the dealers print_results(dealer_hand, player_hand) # shows dealers/ your hand print ("Sorry. Your score isn't higher than the dealer. You lose. ") elif total(player_hand) > total(dealer_hand): # if your hand is higher than the dealers print_results(dealer_hand, player_hand) # shows dealers/ your hand print ("Congratulations. Your score is higher than the dealer. You win ")

def game(): choice = 0 clear() print ("WELCOME TO BLACKJACK! ") # welcomes player to game dealer_hand = deal(deck) # deals to dealers player_hand = deal(deck) # deals to player while choice != "q": print ("The dealer is showing a " + str(dealer_hand[0])) # shows what one of the dealers card is print ("You have a " + str(player_hand) + " for a total of " + str(total(player_hand))) # shows you your hand blackjack(dealer_hand, player_hand) # send inormation to blackjack funciton choice = raw_input("Do you want to [H]it, [S]tand, or [Q]uit: ").lower() # gets user input on if they want to hit, stand, or quit clear() if choice == "h": hit(player_hand) # adds a new card value to your exisiting han d while total(dealer_hand) < 17: # dealer has to hit is value of cards below 17 hit(dealer_hand) score(dealer_hand, player_hand) play_again() elif choice == "s": while total(dealer_hand) < 17: hit(dealer_hand) score(dealer_hand, player_hand) play_again() elif choice == "q": print("Bye!") exit()

if __name__ == "__main__": game()`

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!