【问题标题】:Python Blackjack Functions and Inputs ArrangementPython Blackjack 函数和输入安排
【发布时间】:2020-07-05 18:00:56
【问题描述】:

此代码应生成二十一点手,记分,并计算软 A 的数量。它应该根据游戏类型停止或继续。代码排列不正确,并保持“总”值和/或“手”为空,从而产生无限的 while 循环。如何安排代码以使其返回所需的结果?

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)

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


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."```


【问题讨论】:

  • 我认为你可以在你问用户的问题上做得更好......

标签: python while-loop infinite-loop


【解决方案1】:

第一个 while 循环将永远持续,因为您从未适应 total,但还有其他几个问题:

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

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

  • 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           

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

【讨论】:

  • 谢谢!这是让它走上正轨。我需要让软游戏站在 17 上。我该如何实现?我正在考虑在 if 语句中添加另一个条件:if game_type == 'soft' and card == 1 and total &lt;=17
  • 如果您将带有total += card 的行移到if 之前,那将是有意义的。但是在if 块内,您必须在total 中再添加10 个。我刚刚更新了我的答案中的代码。
  • 我很想知道为什么你会接受total 是(例如)16 进入if 块,因为那样你就被淘汰了。不应该是total &lt;= 11吗?
  • 是的,应该。我试过了,它模拟了 17 的立场。谢谢!
  • 您不应添加其他问题。如果您真的无法让模拟的想法发挥作用,您应该发布一个专门针对该问题的新问题。
猜你喜欢
  • 2018-06-08
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 2014-01-09
  • 2023-01-06
  • 2015-11-03
  • 2015-03-23
  • 2012-07-22
相关资源
最近更新 更多