2012-07-31 62 views
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)。我該如何解決?它應該只給我手中牌的價值。我在這裏做錯了什麼?我的代碼無法正常運行,那裏出了什麼問題?

+14

OMG,你真的需要重構你的代碼 – 2012-07-31 23:09:21

+1

它的格式正確。這只是...獨特的書面。 – 2012-07-31 23:10:10

+0

是的,我很確定你們覺得這很有趣(不是冒犯了),我真的不知道如何讓它工作,因爲我嘗試了其他方式,但對我而言效果並不好。我知道它應該返回一個合適的值,但它不會 – Eliza 2012-07-31 23:13:17

回答

2

編輯使答案更教學。

使用dict來存儲每張卡的分數。

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

然後你就可以在hard遍歷每個card,查找它的價值在字典中,總結分數總給人。

你可能想看看sum函數在這裏可能很有用。

+4

...你做了一件事,那完全不是教誨,因爲它對我來說似乎是一種家庭作業,而且海報應該已經被指示如何爲他/她自己做,而不是讓它解決...... – 2012-07-31 23:17:45

+0

是的,那如果你問我 – 2012-07-31 23:23:13

+0

大聲笑,我認爲他/她已經走了;) – 2012-07-31 23:26:23

5

你行:

for hand in player_cards1: 

可能不是做你期望它做什麼。看起來你認爲它會比較一手牌和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 

我'd也建議重新考慮你跟蹤卡片價值的方式,因爲這會非常麻煩。 。 。提示:只有少數特殊情況下,您不能使用該卡的面值。

+0

這是完美的回答:) – 2012-07-31 23:59:22

相關問題