首页 > 解决方案 > python二十一点王牌问题,它破坏了我的代码

问题描述

我目前正在制作一个 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()

标签: python

解决方案


if "A" in player一旦牌组中有一张王牌,永远True都会出现,所以你永远不会到达else你打印“BUST!”的地方。并返回,所以循环继续。您可以对count牌组中的每个 ace 执行递增操作,然后将 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不是“击中”而不是“站立”。Anor将其两个输入都视为bool值并返回是否至少有一个是True!=运算符优先于,因此该or行实际上是if (action_taken != "hit") or "stand"。它的左边部分做了它应该做的事情,但是右边部分将“stand”评估为 a bool,并且在 python 中,每个非空字符串都被评估为True。所以正确的表达式总是Trueor- 也是如此,程序总是会进入if语句。

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


推荐阅读