This is the code. I'm learning Python and I tried writing a Blackjack script that can calculate the odds of ending up with an X sized hand while having 21 or below. The code is supposed to keep appending random cards out of the deck until it reaches a set amount of cards. (I know decks aren't usually infinite, it's just an experiment)
If it goes over 21 before X amount of cards in hand it discards the hand and starts a new hand.
But for some reason every time it starts a new hand that hand list starts with the same first integer as all previous hands like this:
Can someone please explain to me what I'm doing wrong?
[8, 3, 2, 6, 2]
[8, 2, 3, 2, 5]
[8, 2, 2, 4, 3]
[8, 6, 2, 2, 2]
[8, 2, 2, 2, 7]
[8, 4, 5, 2, 2]
[8, 5, 2, 2, 2]
[8, 2, 2, 2, 6]
[8, 2, 2, 6, 3]
[8, 3, 6, 2, 2]
[8, 6, 2, 2]
total amount of hands: 1020
the percentage of 7 card hands that total 21 or less is 1 in 10.2
import random
card_deck = [2,3,4,5,6,7,8,9,10,11]
def blackjack_tracker(deck):
hand = []
trackers = 0
count = 0
ace = 0
x = 0
while trackers <10:
if len(hand) < 7:
hand.append(random.choice(deck))
if len(hand) == 7 and sum(hand) <= 21:
print(hand)
hand.pop(all(hand))
trackers += 1
count += 1
elif len(hand) == 7 and sum(hand) > 21:
hand.pop(all(hand))
count += 1
print(hand)
print('total amount of hands: {}'.format(count))
print('the percentage of 7 card hands that total 21 or less is 1 in {}'.format(count/10))
blackjack_tracker(card_deck)
Aucun commentaire:
Enregistrer un commentaire