亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果在不同時間運行程序后出現 IndexError 但出現在不同位置,這意味著什么?

如果在不同時間運行程序后出現 IndexError 但出現在不同位置,這意味著什么?

眼眸繁星 2022-11-18 20:56:13
我正在嘗試為我自己的自我發展項目創建 John Conway 的人生游戲。我遇到的一般問題是讓動畫在 GUI 上可視化,現在我收到的錯誤消息如下:Exception in Tkinter callbackTraceback (most recent call last):  File "D:\Software\Python\lib\tkinter\__init__.py", line 1705, in __call__    return self.func(*args)  File "gameoflife.py", line 70, in one_cycle    apply_rules()  File "gameoflife.py", line 56, in apply_rules    updated_grid[row][column] = 0IndexError: list index out of range但是如果我第二次運行它,我會在它最初說錯誤的下面的不同行上得到同樣的錯誤。我知道實際的錯誤是告訴我我試圖訪問的索引不在列表中,但我不明白為什么它會出現在下面的行中,就好像上一行已被更正一樣。我的代碼如下:from tkinter import *from random import *import timeimport numpy as npPIXEL_SIZE = 10ROW = 910COLUMN = 700grid = []updated_grid = [[]]def create_grid():    for row in range(0, ROW):        grid2 = []        for column in range(0, COLUMN):            grid2.append(randint(0, 1))        grid.append(grid2)def draw_grid():    for row in range(0, ROW):        for column in range(0, COLUMN):            if grid[row][column] == 1:                x0 = row*PIXEL_SIZE                y0 = column*PIXEL_SIZE                x1 = x0+PIXEL_SIZE                y1 = y0+PIXEL_SIZE                canvas.create_rectangle(x0, y0, x1, y1, fill='red')def apply_rules():    for row in range(1, ROW - 1):        for column in range(1, COLUMN - 1):            neighbours_count = 0            # will count the neighbours for each cell            neighbours_count += grid[row-1][column-1] # top left            neighbours_count += grid[row][column-1] # top center            neighbours_count += grid[row+1][column-1] # top right            neighbours_count += grid[row-1][column] # middle left            neighbours_count += grid[row+1][column] # middle right            neighbours_count += grid[row-1][column+1] # bottom left            neighbours_count += grid[row][column+1] # bottom center            neighbours_count += grid[row+1][column+1] # bottom right
查看完整描述

2 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

你從未填寫過updated_grid,所以你不能分配給它的元素。


您應該在程序啟動時創建兩個網格。


def create_grid(ROW, COLUMN):

    grid = []

    for row in range(0, ROW):

        grid2 = []

        for column in range(0, COLUMN):

            grid2.append(randint(0, 1))

        grid.append(grid2)

    return grid


grid = create_grid(ROW, COLUMN)

updated_grid = create_grid(ROW, COLUMN)


查看完整回答
反對 回復 2022-11-18
?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

最簡單的解決方案是復制現有的網格并在以后使用該網格:


import copy


def apply_rules():

    global grid

    updated_grid = copy.deepcopy(grid)

    # the rest of the function here, except the copying back again


    # This is all that's needed to 'copy' it back again:

    grid = updated_grid

這樣,您從網格的副本開始:( copy.deepcopy(grid)) 并像您一樣覆蓋元素:( 例如updated_grid[row][column] = 0) 最后處理舊網格并將新網格保持在一行中:( grid = updated_grid) 通過引用計數的魔力.


這是一種形式double buffering。


查看完整回答
反對 回復 2022-11-18
  • 2 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號