2016-05-16 69 views
-2

我有問題,當調用我的功能,我收到不好的輸出,因爲我會在文中稍後解釋。異常和輸出

這些都是我一起工作的資源:

Main_Building=[10,12,14,17,21,25,30,36,43,52,62,74,89,107,128,154,185,222,266,319,383,460,552,662,795,954,1145,1374,1648,1978] 
Barracks=[16,19,23,28,33,40,48,57,69,83,99,119,143,171,205,247,296,355,426,511,613,736,883,1060,1272] 
Stables=[20,24,29,35,41,50,60,72,86,103,124,149,178,214,257,308,370,444,532,639] 
Workshop=[24,29,35,41,50,60,72,86,103,124,149,178,214,257,308] 
Blacksmith=[19,23,27,33,39,47,57,68,82,98,118,141,169,203,244,293,351,422,506,607] 
Market=[10,12,14,17,21,25,30,36,43,52,62,74,89,107,128,154,185,222,266,319,383,460,552,662,795] 
Axe=[6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989,1187] 
Clay_Pit=[6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989,1187] 
Mine=[6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989,1187] 
Settler_House=[5,6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989] 
Warehouse=[6,7,9,10,12,15,18,21,26,31,37,45,53,64,77,92,111,133,160,192,230,276,331,397,477,572,687,824,989,1187] 
Wall=[8,10,12,14,17,20,24,29,34,41,50,59,71,86,103,123,148,177,213,256] 

這裏是我的代碼:

def buildings(points): 
    for i in range(0,30): 
     try: 
      if Main_Building[i]>=points: 
       del Main_Building[i:] 
      if Barracks[i]>=points: 
       del Barracks[i:] 
      if Stables[i]>=points: 
       del Stables[i:] 
      if Workshop[i]>=points: 
       del Workshop[i:] 
      if Blacksmith[i]>=points: 
       del Blacksmith[i:] 
      if Market[i]>=points: 
       del Market[i:] 
      if Axe[i]>=points: 
       del Axe[i:] 
      if Clay_Pit[i]>=points: 
       del Clay_Pit[i:] 
      if Mine[i]>=points: 
       del Mine[i:] 
      if Settler_House[i]>=points: 
       del Settler_House[i:] 
      if Warehouse[i]>=points: 
       del Warehouse[i:] 
      if Wall[i]>=points: 
       del Wall[i:]     
     except IndexError: 
      continue 

的問題是當它涉及到鐵匠的條件,在我看來作爲條件只能通過,而其他人則持續到Wall條件。條件是確定停止和刪除列表的其餘部分以供進一步使用。這些列表的長度是不同的,所以我使用了簡單的例外,當它超出範圍時,它會跳過並繼續下一個條件。

建議時輸出高清建築(100):

Blacksmith=[19,23,27,33,39,47,57,68,82,98] 

實際產量沒有任何改變整個列表。這同樣適用於持續狀況。

我的嘗試:

  1. 我試圖重新啓動的Python,但是,不幸的是沒有了。

  2. 如果拼寫錯了變量名稱。

  3. 重做每個條件的間距。

也許解決方案,但不是有效的,增加到每個條件嘗試異常?(在我看來不是一個好主意)。

爲什麼它跳過了條件?

謝謝你的幫助和時間。

回答

0

Continue將返回到當前循環的開始並抓取下一個索引。這意味着如果中途發生錯誤,則會跳過後半部分。這也很長。這是我的解決方案。

data = [Main_Building, Barracks, Stables, Workshop, Blacksmith, Market, Axe, Clay_Pit, Mine, Settler_House, Warehouse, Wall] 
def buildings(points): 
    for building in data: # loop through each building seperately, shortens code and removes arbitrary loop number 
     for point in building: # loop through each buildings index, same as your code 
      if point >= points: 
       del building[building.index(point):] 
buildings(20) 
print data 
+0

謝謝,現在我明白了。我犯了多麼糟糕的錯誤。 –

0

如果在try-except之間發生任何錯誤,Python將通過或繼續try-except之間的所有代碼。

try: 
    a = int(input("integer: ")) 
    print("print this") 
    print("print that") 
except: 
    pass 

輸出:

>>> 
integer: ab 
>>> 

請參閱print thisprint that不打印。你應該逐個捕捉錯誤。

try: 
    a = int(input("integer: ")) 
except: 
    pass 
print("print this") 
print("print that") 

>>> 
integer: ab 
print this 
print that 
>>>