【问题标题】:python blackjack ace problem which breaks my codepython二十一点王牌问题,它破坏了我的代码
【发布时间】:2020-03-04 13:28:28
【问题描述】:

我目前正在制作一个 21 点蟒蛇迷你游戏,玩家在其中玩到他得到 21 点或站起来并返回他的牌组的价值。该游戏不与庄家对战,而只是使用他的手作为得分。我的问题是,我想不出一种方法来让 A 从 11 变为 1 而不循环和破坏程序。这是我的代码:

   import random


def play():
    output = "Your hand: "
    player = []
    total=0
    count = 0
    while len(player) != 2:
        card = random.randint(1,52)
        if card <= 4:
            output += "A "
            total += 11
            player.append("A")
        elif card <= 8:
            output+="2 "
            total+=2
            player.append("2")
        elif card <= 12:
            output+="3 "
            total+=3
            player.append("3")
        elif card <= 16:
            output+="4 "
            total+=4
            player.append("4")
        elif card <= 20:
            output+="5 "
            total+=5
            player.append("5")
        elif card <= 24:
            output+="6 "
            total+=6
            player.append("6")
        elif card <= 28:
            output+="7 "
            total+=7
            player.append("7")
        elif card <= 32:
            output+="8 "
            total+=8
            player.append("8")
        elif card <= 36:
            output+="9 "
            total+=9
            player.append("9")
        elif card <= 40:
            output+="10 "
            total+=10
            player.append("10")
        elif card <= 44:
            output+="J "
            total+=10
            player.append("J")
        elif card <= 48:
            output+="Q "
            total+=10
            player.append("Q")
        elif card <= 52:
            output+= "K "
            total+=10
            player.append("K")
        if len(player) == 2:
            print(f"{output} ({total}) ")

    while len(player) >= 2:
        action_taken = input("Would you like to 'hit' or 'stand': ")
        if action_taken == "hit":
            card = random.randint(1,52)
            if card <= 4:
                output += "A "
                total += 11
                player.append("A")
            elif card <= 8:
                output+="2 "
                total+=2
                player.append("2")
            elif card <= 12:
                output+="3 "
                total+=3
                player.append("3")
            elif card <= 16:
                output+="4 "
                total+=4
                player.append("4")
            elif card <= 20:
                output+="5 "
                total+=5
                player.append("5")
            elif card <= 24:
                output+="6 "
                total+=6
                player.append("6")
            elif card <= 28:
                output+="7 "
                total+=7
                player.append("7")
            elif card <= 32:
                output+="8 "
                total+=8
                player.append("8")
            elif card <= 36:
                output+="9 "
                total+=9
                player.append("9")
            elif card <= 40:
                output+="10 "
                total+=10
                player.append("10")
            elif card <= 44:
                output+="J "
                total+=10
                player.append("J")
            elif card <= 48:
                output+="Q "
                total+=10
                player.append("Q")
            elif card <= 52:
                output+= "K "
                total+=10
                player.append("K")
            if len(player) >= 2 and total <=21:
                print(f"{output} ({total}) ")

        if total > 21:

            if "A" in player: #Ask why ace always messes up
                if count < 1:
                    count +=1
                    total-=10
                    print(f"{output} ({total}) ")
                if player.count("A") > 1:
                    total -= 10
                    print(f"{output} ({total}) ")
            else:
                print(f"{output} ({total}) ")
                print("BUST!")
                return total

        if action_taken == "stand":
            return total

        if action_taken != "hit" or "stand":
            print("Enter a valid input ('hit' or 'stand') ")


play()

【问题讨论】:

  • 拿到 A 时有哪些问题症状?

标签: python


【解决方案1】:

if "A" in player 将永远是True,一旦牌组中有一张 A,因此您永远无法到达打印“BUST!”的else。并返回,所以循环继续。您可以对牌组中的每个 ace 执行递增 count 之类的操作,然后将 ace 部分更改为:

    if total > 21:
        player_aces = player.count("A")  # How many aces the player has
        if player_aces != count: # Meaning there are aces that weren't checked
            for _ in range(player_aces - count):
                total -= 10  # Could also be simplified to: total -= 10 * (player_aces - count)
            count = player_aces
            print(f"{output} ({total}) ")
        else:
            print(f"{output} ({total}) ")
            print("BUST!")
            return total

另外,if action_taken != "hit" or "stand" 不会检查 action_taken 不是“击中”也不是“站立”。 or 将其两个输入都视为bool 值,并返回是否至少有一个是True!= 运算符优先于 or,因此该行实际上是 if (action_taken != "hit") or "stand"。左边部分做了它应该做的事情,但右边部分将“stand”评估为bool,在python中,每个非空字符串都被评估为True。所以正确的表达式总是Trueor也是如此——程序总是会进入if语句。

您可能想要:if action_taken != "hit" and action_taken != "stand"

【讨论】:

  • 你能解释一下你所说的检查站和它不是一个空字符串是什么意思吗?我不太确定我是否理解。
【解决方案2】:

我已经解决了一些问题。我为 A 的数量创建了一个计数器,这使我们可以计算可以将总数减少多少次 - 否则我们只会继续删除 10。

最后一个 else 语句的缩进也需要移出。

import random


def play():
    output = "Your hand: "
    player = []
    total=0
    count = 0
    reducedA = 0
    while len(player) != 2:
        card = 1
        #card = random.randint(1,52)
        if card <= 4:
            output += "A "
            total += 11
            reducedA+=1
            player.append("A")
        elif card <= 8:
            output+="2 "
            total+=2
            player.append("2")
        elif card <= 12:
            output+="3 "
            total+=3
            player.append("3")
        elif card <= 16:
            output+="4 "
            total+=4
            player.append("4")
        elif card <= 20:
            output+="5 "
            total+=5
            player.append("5")
        elif card <= 24:
            output+="6 "
            total+=6
            player.append("6")
        elif card <= 28:
            output+="7 "
            total+=7
            player.append("7")
        elif card <= 32:
            output+="8 "
            total+=8
            player.append("8")
        elif card <= 36:
            output+="9 "
            total+=9
            player.append("9")
        elif card <= 40:
            output+="10 "
            total+=10
            player.append("10")
        elif card <= 44:
            output+="J "
            total+=10
            player.append("J")
        elif card <= 48:
            output+="Q "
            total+=10
            player.append("Q")
        elif card <= 52:
            output+= "K "
            total+=10
            player.append("K")
        if len(player) == 2:
            print(f"{output} ({total}) ")

    while len(player) >= 2:
        action_taken = input("Would you like to 'hit' or 'stand': ")
        if action_taken == "hit":
            card = random.randint(1,52)
            if card <= 4:
                output += "A "
                total += 11
                player.append("A")
                reducedA += 1
            elif card <= 8:
                output+="2 "
                total+=2
                player.append("2")
            elif card <= 12:
                output+="3 "
                total+=3
                player.append("3")
            elif card <= 16:
                output+="4 "
                total+=4
                player.append("4")
            elif card <= 20:
                output+="5 "
                total+=5
                player.append("5")
            elif card <= 24:
                output+="6 "
                total+=6
                player.append("6")
            elif card <= 28:
                output+="7 "
                total+=7
                player.append("7")
            elif card <= 32:
                output+="8 "
                total+=8
                player.append("8")
            elif card <= 36:
                output+="9 "
                total+=9
                player.append("9")
            elif card <= 40:
                output+="10 "
                total+=10
                player.append("10")
            elif card <= 44:
                output+="J "
                total+=10
                player.append("J")
            elif card <= 48:
                output+="Q "
                total+=10
                player.append("Q")
            elif card <= 52:
                output+= "K "
                total+=10
                player.append("K")
            if len(player) >= 2 and total <=21:
                print(f"{output} ({total}) ")

        if total > 21:

            if "A" in player: #Ask why ace always messes up
                if count < 1:
                    count +=1
                    total-=10
                    print(f"{output} ({total}) ")
                if player.count("A") > 1 and reducedA:
                    total -= 10
                    reducedA -= 1
                    print(f"{output} ({total}) ")
                else:
                    print(f"{output} ({total}) ")
                    print("BUST!")
                    return total
            else:
                print(f"{output} ({total}) ")
                print("BUST!")
                return total

        if action_taken == "stand":
            return total

        if action_taken != "hit" or action_taken != "stand":
            print("Enter a valid input ('hit' or 'stand') ")


play()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-09
    • 2020-03-06
    • 2017-09-30
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多