Python 二十一点功能和输入安排

Python Blackjack Functions and Inputs Arrangement

这段代码应该生成一手二十一点,记录分数,并计算软 A 的数量。它应该停止或继续取决于游戏的类型。代码排列不正确,将“总计”值 and/or“手”保持为空,从而导致无限循环。我该如何安排代码以 returns 获得预期的结果?

import random

def get_card():
    #hand = []
    card = random.randint(1, 13)
    if card == 10 or card == 11 or card == 12 or card == 13:
        card = 10
    #hand.append(card)
    #return hand
    return card

def score():
    """
    keeps score of the hand and counts saft aces.
    """
    hand = []
    card = get_card()
    #hand = [get_card()]
    game_type = input("Enter 'soft'for S17 ot 'hard for H17'.")
    total = 0
    soft_ace_count = 0
    for ele in range(0, len(hand)):
        total = total + hand[ele]

    while len(hand) <= 5:
        while total <= 17 and game_type == 'soft':
            if card == 1:
                card = 11
                hand.append(card)

        while total <= 17 and game_type == 'hard':
            if card == 1:
                hand.append(card)

        if ele in hand == 11:
                soft_ace_count += 1

    return(total, soft_ace_count)

我还想将程序模块化,并根据一些用户定义的模拟来计算失败的概率。我不知道如何设置模拟for循环来临时保存模拟结果以计算概率。我应该将它们放在临时文件中吗?


def main():
    try:
        num_simulations = int(input("Please enter the desired number of simulations: "))
    except ValueError:
        print("Please enter an integer value for the nummer of simulations.")

    try:
        stand_on_value = int(input("Please enter a score value to stand on. "))
    except ValueError:
        print("Please enter an integer value for the stand-on score.")

    try:
        game_type = input("Enter 'soft' for S17 or 'hard' for H17.")
    except ValueError:
        if game_type != 'soft' or game_type != 'hard':
            print("Please enter 'soft' or 'hard'.")

    for i in range(0, num_simulations):
        get_card()
        score()

    "for-loop to calculate probability of busting as the percentage of busted hands in the simulations."```


第一个while循环将永远持续下去,因为你永远不会适应total,但还有其他几个问题:

  • while 中的 if 条件为假时,手也不会伸出,也不会发生任何其他情况。

  • 每当循环迭代时,您应该select一张新卡

  • for ele in range(0, len(hand))目前放的地方没用,因为手上没有牌

  • if ele in hand == 11 没有按照您的意愿行事。应该是if 11 in hand。更好的是当你实际将卡设置为 11 时才执行此工作。然后不再需要额外的 if

  • 由于两个循环中只有一个while循环条件永远为真,因此只使用一个while循环并在其中进行区分。

改为:

def score():
    game_type = input("Enter 'soft' for S17 or 'hard' for H17.")
    hand = []
    total = 0
    soft_ace_count = 0
    while total <= 17:
        card = get_card()
        total += card # you must bring total up to date here
        if game_type == 'soft' and card == 1 and total <= 11:
            card = 11
            total += 10  # adapt to the new value of the card
            soft_ace_count += 1  # move this here...
        # so you can see what is happening 
        print("Got card {}. Total is {}".format(card, total))  
        hand.append(card) # always append...
    # Do you plan to do something with hand? Do you really need it?
    # ...
    return total, soft_ace_count  # parentheses are not needed           

我没有验证你的其余代码,但这至少解决了你的问题。