首页 文章

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

提问于
浏览
0
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) . 我该如何解决?它应该只给我手中牌的 Value . 我在这里做错了什么?

2 回答

  • 2

    你的路线:

    for hand in player_cards1:
    

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

    a = [1,2,3]
    for item in a: //creates an iterator around a, with the variable item to refer to each list member
      print item
    

    这三行计划将输出:

    1
    2
    3
    

    相反,你可能想要迭代手中的牌,例如:

    for card in hand:
      //code here to look up the value of the card and add it to a running total
    

    我还建议重新考虑你跟踪卡片值的方式,因为它会非常麻烦 . . . 提示:只有少数特殊情况下你不能使用卡的面值 .

  • 5

    Edited to make the answer more didactical.

    使用dict存储每张卡的分数 .

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

    然后你可以迭代 hard 中的每个 card ,在字典中查找它的值,并将得分相加得到总数 .

    你可能想看一下这里可能有用的sum函数 .

相关问题