我的代码不能正常运行,有什么问题吗?

2024-09-30 12:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

hand = ['A','Q']
points = 0
player_cards1 = ['2']
value1 = 2
player_cards2 = ['3']
value2 = 3
player_cards3 = ['4']
value3 = 4
player_cards4 = ['5']
value4 = 5
player_cards5 = ['6']
value5 = 6
player_cards6 = ['7']
value6 = 7
player_cards7 = ['8']
value7 = 8
player_cards8 = ['9']
value8 = 9
player_cards9 = ['T']
value9 = 10
player_cards10 = ['Q']
value10 = 10
player_cards11 = ['K']
value11 = 10
player_cards12 = ['J']
value12 = 10
ACE11 = ['A']
value13 = 11

for hand in player_cards1:
    flag = True
    if flag == True:
        points = points + value1
    for hand in ACE11:
        flag = True
        if flag == True:
            points = points + value13
        for hand in player_cards2:
            flag = True
            if flag == True:
                points = points + value2
            for hand in player_cards3:
                flag = True
                if flag == True:
                    points = points + value3
                for hand in player_cards4:
                    flag = True
                    if flag == True:
                        points = points + value4
                    for hand in player_cards5:
                        flag = True
                        if flag == True:
                            points = points + value5
                        for hand in player_cards6:
                            flag = True
                            if flag == True:
                                points = points + value6
                            for hand in player_cards7:
                                flag = True
                                if flag == True:
                                    points = points + value7
                                for hand in player_cards8:
                                    flag = True
                                    if flag == True:
                                        points = points + value8
                                    for hand in player_cards9:
                                        flag = True
                                        if flag == True:
                                            points = points + value9
                                        for hand in player_cards10:
                                            flag = True
                                            if flag == True:
                                                points = points + value10
                                            for hand in player_cards11:
                                                flag = True
                                                if flag == True:
                                                    points = points + value11
                                                for hand in player_cards12:
                                                    flag = True
                                                    if flag == True:
                                                        points = points + value12
                                                    print points

它在前五个街区跑得很好,然后它给了我整副牌的总价值(95)。我怎么解决这个问题?它应该只给我手上牌的价值。我在这里做错了什么?在


Tags: intrueforifpointsflagplayerhand
2条回答

编辑以使答案更具教学性。

使用^{}存储每张卡片的分数。在

card_points = { 'A': 11, '2': 2, ... }

然后,您可以在hard中的每个card进行迭代,在字典中查找其值,并将分数相加,得出总数。在

您可能想看看^{}函数,它在这里可能很有用。在

你的台词:

for hand in player_cards1:

很可能没有做你期望的事情。看起来你认为它会比较hand和player_cards1;相反,它所做的是在player_cards1上创建一个迭代器,您可以通过变量hand访问该迭代器(意味着hand被重新分配,不再是['A','Q'])。下面是一个简单的例子:

^{pr2}$

三行程序将输出:

1
2
3

取而代之的是,你可能需要在手中的卡片上进行迭代,例如:

^{4}$

我也建议你重新考虑一下你追踪信用卡价值的方式,因为这会非常麻烦。提示:只有少数情况下你不能使用信用卡的面值。在

相关问题 更多 >

    热门问题