HW 15

Create a program blackjack.py that will simulate the first 2 cards dealt in a blackjack hand, and simulate N of these hands. You will calculate the average hand value, as well as how many blackjacks were initially dealt (score of 21). We provide you starter code below which includes a cards variable containing a list of the 52 card scores. You don't even need to know Blackjack! Just randomly choose 2 values from our list of cards (if you do know, we simplify blackjack so that Aces are always 11 in this HW).

import random
cards = list(range(2,11))*4 + [10]*12 + [11]*4

def simulate_blackjack(N):
    # Your code here!





    # How to return 2 values -- use a tuple!	  
    # avg = average score over all N runs
    # blackjack_percent = # of 21 scores divided by N
    return (avg,blackjack_percent)



# Do not change anything below this line! But you understand the code below, right?	  
if __name__ == '__main__':
  N = int(input('How many simulations? '))
  (avg,blackjacks) = simulate_blackjack(N)

  print('Blackjack average =', blackjacks)
  print('Average score =', avg)

Your task is to fill in the simulate_blackjack(N) function. Your function must randomly choose two cards from our provided list of card values. Add them to get the hand's score. Repeat this N times in a loop. Keep track of the sum of all hands, and then compute the average hand value at the end of the function. In addition, count how many perfect 21 scores are seen ("blackjack!"). Compute the fraction of blackjacks over all simulations at the end too. Your function should return BOTH of these numbers as a tuple:

return (avg,blackjack_percent)

Running your program should look like this:

python3 blackjack.py
How many simulations? 1000000
Blackjack average = 0.04707
Average score = 14.60407

Submit

Submit your program to the online submission system.

submit -c=sd211 -p=hw15-cards blackjack.py