簡體   English   中英

為什么運行時會出現“pop index out of range”錯誤?

[英]Why do I get "pop index out of range" error when running this?

我正在編寫代碼來為數學游戲“蒲公英”制作一個機器人,為此,我獲取了所有包含數字 1 的列、行和對角線。然后,使用包含數字 1 的列表列表,我需要使用 for 循環將它們分開。 但是,在運行此 for 循環時,我收到一條錯誤消息,指出彈出索引超出范圍,但是當我使用每個單獨的 i 值運行代碼時,我沒有收到錯誤消息。 這是導致此問題的代碼末尾的最后一個 for 循環。 請幫忙! 注意:即使運行范圍為 3 的 for 循環,它仍然會輸出錯誤。 僅輸出索引 0 和 2。

row_1 = [0, 0, 0, 0, 0]
row_2 = [0, 1, 0, 0, 0]
row_3 = [0, 0, 0, 0, 0]
row_4 = [0, 0, 0, 0, 0]
row_5 = [0, 0, 0, 0, 0]

col_1 = [0, 0, 0, 0, 0]
col_2 = [0, 0, 1, 0, 0]
col_3 = [0, 0, 0, 0, 0]
col_4 = [0, 0, 0, 0, 0]
col_5 = [0, 0, 0, 0, 0]

#diagonals 1-9 go from left to right, while diagonals 10-18 go from right to left

dia_1 = [0]
dia_2 = [0, 0]
dia_3 = [0, 0, 0]
dia_4 = [0, 0, 0, 0]
dia_5 = [0, 0, 0, 1, 0]
dia_6 = [0, 0, 0, 0,]
dia_7 = [0, 0, 0]
dia_8 = [0, 0]
dia_9 = [0]

dia_10 = [0]
dia_11 = [0, 0]
dia_12 = [0, 0, 0]
dia_13 = [0, 0, 0, 0]
dia_14 = [0, 0, 0, 0, 1]
dia_15 = [0, 0, 0, 0,]
dia_16 = [0, 0, 0]
dia_17 = [0, 0]
dia_18 = [0]

dia = [dia_1, dia_2, dia_3, dia_4, dia_5, dia_6,
       dia_7, dia_8, dia_9, dia_10, dia_11, dia_12,
       dia_13, dia_14, dia_15, dia_16, dia_17, dia_18]
row = [row_1, row_2, row_3, row_4, row_5]
col = [col_1, col_2, col_3, col_4, col_5]
possible = []

for i in dia:
    if 1 in i:
        possible.append(i)

for i in row:
    if 1 in i:
        possible.append(i)

for i in col:
    if 1 in i:
        possible.append(i)

for i in range(0, 4):
    print(possible.pop(i))

為您提供超出范圍錯誤的 for 循環將它們包裝在 try-except 塊中,在異常中添加新邏輯以忽略或修復超出范圍; 例如:

try:
    for i in range(0, 4):
        print(possible.pop(i))
except:
    #correct your out-of-range:
    pass

似乎你沒有得到我的提示,到目前為止唯一的答案不是很有幫助。

為了重現這個問題,讓我們對possible的進行硬編碼,因為最后一個循環之前的所有內容都無關緊要。

possible = [[0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]
print(f"Possible before loop: {possible}")
for i in range(0, 4):
    print(f"Item at pos {i}: {possible.pop(i)}")
    print(f"Possible after pop: {possible}")

輸出:

Possible before loop: [[0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]
Item at pos 0: [0, 0, 0, 1, 0]
Possible after pop: [[0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]
Item at pos 1: [0, 1, 0, 0, 0]
Possible after pop: [[0, 0, 0, 0, 1], [0, 0, 1, 0, 0]]
Traceback (most recent call last):
  File "test.py", line 5, in <module>     
    print(f"Item at pos {i}: {possible.pop(i)}")
IndexError: pop index out of range

解釋:

正如我之前所說, i一直在增加,但與此同時,您正在從possible的項目中刪除項目。

在第一次迭代之后possible[[0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]

但是在下一次(第二次)迭代期間,您將在索引1處彈出項目,即[0, 1, 0, 0, 0] - 您可能期待[0, 0, 0, 0, 1]

在第三次迭代期間,可能只包含兩個項目: [[0, 0, 0, 0, 1], [0, 0, 1, 0, 0]]但您試圖在索引2處彈出項目,即當pop index out of range錯誤發生時。

為什么不簡單地迭代您的列表並打印項目而不是彈出它們?

possible = [[0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]
for item in possible:
    print(item)

輸出:

[0, 0, 0, 1, 0]
[0, 0, 0, 0, 1]
[0, 1, 0, 0, 0]
[0, 0, 1, 0, 0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM