python 21点ace问题,它破坏了我的代码

python 21点ace问题,它破坏了我的代码,python,Python,我目前正在制作一个21点python小游戏,玩家玩到21点或者站起来并返回他的牌组的价值。这场比赛不是和庄家比赛,而是用他的手作为分数。我的问题是,我无法找到一种方法,使ACE从11变为1,而不循环并破坏程序。这是我的密码: import random def play(): output = "Your hand: " player = [] total=0 count = 0 while len(player) != 2: c

我目前正在制作一个21点python小游戏,玩家玩到21点或者站起来并返回他的牌组的价值。这场比赛不是和庄家比赛,而是用他的手作为分数。我的问题是,我无法找到一种方法,使ACE从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()
随机导入
def play():
output=“您的手:”
玩家=[]
总数=0
计数=0
而莱恩(球员)!=2:
card=random.randint(1,52)

如果卡有一些问题我已经解决了。我已经为ACE的数量创建了一个计数器,这允许我们计算我们可以减少总数多少次,否则我们只需要继续删除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()
随机导入
def play():
output=“您的手:”
玩家=[]
总数=0
计数=0
reducedA=0
而莱恩(球员)!=2:
卡=1
#card=random.randint(1,52)

如果卡有一些问题我已经解决了。我已经为ACE的数量创建了一个计数器,这允许我们计算我们可以减少总数多少次,否则我们只需要继续删除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()
随机导入
def play():
output=“您的手:”
玩家=[]
总数=0
计数=0
reducedA=0
而莱恩(球员)!=2:
卡=1
#card=random.randint(1,52)
如果牌组中有一张王牌,玩家中的“A”将始终为“真”,因此你永远无法进入打印“半身像”并返回的
,因此循环将继续。您可以对组中的每个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
另外,如果采取了行动,
“击中”或“站立”
不会检查所采取的
操作是否不是“击中”或“站立”。
将其两个输入都视为
bool
值,并返回是否至少有一个为
=
运算符的优先级高于
,因此该行实际上是
if(action_taked!=“hit”)或“stand”
。它的左半部分执行它应该执行的操作,但右半部分将“stand”计算为
bool
,在python中,每个非空字符串都计算为
True
。因此正确的表达式总是
True
,而
-也是如此,程序将始终输入
if
语句

您可能希望:
如果采取了操作!=“击中”并采取行动!=“支架”

如果玩家中的“A”
在牌组中有一张王牌时总是
正确的
,因此你永远不会到达
其他
打印“半身像!”并返回,因此循环继续。您可以对组中的每个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
另外,如果采取了行动,
“击中”或“站立”
不会检查所采取的
操作是否不是“击中”或“站立”。
将其两个输入都视为
bool
值,并返回是否至少有一个为
=
运算符的优先级高于
,因此该行实际上是
if(action_taked!=“hit”)或“stand”
。它的左半部分执行它应该执行的操作,但右半部分将“stand”计算为
bool
,在python中,每个非空字符串都计算为
True
。因此正确的表达式总是
True
,而
-也是如此,程序将始终输入
if
语句


您可能希望:
如果采取了操作!=“击中”并采取行动!=“支架”

当你得到A牌时,有什么问题的症状?当你得到A牌时,有什么问题的症状?你能解释一下你所说的它不是一个空字符串是什么意思吗?我不太清楚我是否理解。你能解释一下你所说的它不是空字符串是什么意思吗?我不太清楚我是否理解。