【问题标题】:My code doesn't run properly, what is wrong in there?我的代码运行不正常,那里有什么问题?
【发布时间】:2012-07-31 23:08:29
【问题描述】:
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)。我该如何解决?它应该只给我手牌的价值。我在这里做错了什么?

【问题讨论】:

  • 天啊,你真的需要重构你的代码
  • 格式正确。它只是......写得独一无二。
  • 是的,我很确定你们会觉得这很有趣(没有被冒犯),老实说,我不知道如何让它发挥作用,因为我尝试了其他方法,但效果不佳为了我。我知道它应该返回一个正确的值,但它没有
  • 真的,我一点也不觉得有趣,也不是在开玩笑。我有一条经验法则:每当我发现自己在编写嵌套循环时,如果达到第三个,可能是时候进行一些重构了。也许你可以解释一下你到底想做什么。

标签: python list python-2.7


【解决方案1】:

您的线路:

for hand in player_cards1:

可能没有做你期望它做的事情。看起来您认为它将比较 hand 和 player_cards1;相反,它所做的是在 player_cards1 上创建一个迭代器,您可以通过变量 hand 访问该迭代器(这意味着该手被重新分配,并且将不再是 ['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

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

【讨论】:

  • 这是完美的答案:)
【解决方案2】:

已编辑以使答案更具指导性。

使用dict 存储每张卡片的分数。

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

然后您可以遍历hard 中的每个card,在字典中查找它的值,然后将分数相加得到总数。

您可能想查看这里可能有用的 sum 函数。

【讨论】:

  • ...你做了一些事情,这完全不是教学性的,因为这对我来说似乎是一项家庭作业,并且应该指导发帖人如何为他/她自己做这件事,而不是让它解决...
  • @MarkByers 好吧,不是来自我。我不是在开玩笑
  • 好吧,对不起,我不像你们中的一些人那样是计算机天才,我是生物化学专业的,这门课程是必修课。您不必为我写出答案,只需提出如何做的建议即可。
  • @Miroslav Hudak:我真的很无聊,所以我要增加体重,因为......好吧,因为我可以。 Mark Byers 的回答对我来说看起来很具有指导意义......没有具体的实现,只是指出要做什么......问题的海报仍然有很多工作要做(嘿,恕我直言)。此外,这个网站是问/答问题,不一定是编程。我相信课程教授的任务是检查他/她的学生是否真的知道编程(不知道......可能通过对作业提出问题)......伙计,我很无聊!跨度>
  • @Borrajax - 你应该在编辑之前检查他的答案是什么,也就是发表非教学评论的时候。 . .
猜你喜欢
  • 2021-10-24
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
  • 2018-06-22
相关资源
最近更新 更多