这是代码。我正在学习 Python,我尝试编写一个 Blackjack 脚本,该脚本可以计算在拥有 21 点或以下的情况下以 X 大小的手结束的几率。该代码应该不断地从牌组中添加随机卡片,直到达到一定数量的卡片。(我知道甲板通常不是无限的,这只是一个实验)
如果它在 X 手牌数量之前超过 21,则弃牌并开始新手牌。
但出于某种原因,每次它开始新手牌时,手牌列表都以与之前所有手牌相同的第一个整数开头,如下所示:
有人可以向我解释我做错了什么吗?
[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)
神不在的星期二
相关分类