I wrote a piece of blackjack code - for dealer dealing cards to himself (python). My question is how can I make it more efficient, faster and neater. I ran it for 1.000.000 iterations and it took 14.7 second which is quite slow, I believe.
import random
deck = 4 * [2,3,4,5,6,7,8,9,10,10,10,10,11]
random.shuffle(deck)
dealer_hand = []
dealer_hand.append(deck.pop(0))
dealer_hand.append(deck.pop(0))
hit_on_soft_17 = True
exit = False
while not exit:
if sum(dealer_hand) == 17 and hit_on_soft_17:
exit = True
for i, card in enumerate(dealer_hand):
if card == 11:
exit = False
dealer_hand.append(deck.pop(0))
break
if sum(dealer_hand) < 17:
exit = False
dealer_hand.append(deck.pop(0))
if sum(dealer_hand) > 21:
exit = True
for i, card in enumerate(dealer_hand):
if card == 11:
exit = False
dealer_hand[i] = 1
break
if sum(dealer_hand) < 22 and sum(dealer_hand) > 17:
exit = True
print(dealer_hand)