我正在用 Python 编写 Conway 的生活游戏,但偶然发现了一个奇怪的语法错误。它在我定义的第一个函数中。据说nextnext=[]中的nextnext是错误的。如果我把它拿出来:在for循环之后也会给出一个synthax错误。我在一个单独的文件中尝试了这个函数,它在那里工作得很好,所以我真的不知道我做错了什么。
TIME = 100
life = [] # create list 'life'
life.append(seed) # add seed to life
numrows = len(seed) # calculate number of rows
numcolumns = len(seed[0]) # calculate number of columns
current = seed # make seed the first current(matrix you're starting off with in each step)
nextnext = []
def create_empty_universum (seed):
numrows = len(seed) # calculate number of rows
numcolumns = len(seed[0] # calculate number of columns
nextnext = [] # create empty list next
for i in range(numrows): # define number of rows in nextnext
nextnext.append([0] * numcolumns) # define number of columns in nextnext
return nextnext
def compute_new_value(nextnext,row,column):
neighbors = 0 # start counter 'neighbors' with 0
if current[row][(column+1)%numcolumns] == 1: # if neighboring cell has a value of 1
neighbors += 1 # add 1 to neighbors
if current[row][(column-1)%numcolumns] == 1: # if neighboring cell has a value of 1
neighbors += 1 # add 1 to neighbors
if current[(row-1)%numrows][column] == 1: # if neighboring cell has a value of 1
neighbors += 1 # add 1 to neighbors
if current[(row+1)%numrows][column] == 1: # if neighboring cell has a value of 1
neighbors +=1 # add 1 to neighbors
if current[(row+1)%numrows][(column+1)%numcolumns] == 1: # if neighboring cell has a value of 1
neighbors +=1 # add 1 to neighbors
if current[(row-1)%numrows][(column+1)%numcolumns] == 1: # if neighboring cell has a value of 1
neighbors += 1 # add 1 to neighbors
if current[(row-1)%numrows][(column-1)%numcolumns] == 1: # if neighboring cell has a value of 1
neighbors += 1 # add 1 to neighbors
if current[(row+1)%numrows][(column-1)%numcolumns] == 1: # if neighboring cell has a value of 1
neighbors +=1 # add 1 to neighbors
if current[row][column] == 1: # in case a target cell has a value of 1:
if neighbors < 2: # if the number of neighbors is smaller than 2
nextnext[row][column] = 0 # value of target cell becomes 0 in nextnext
elif neighbors == 2 or neighbors == 3: # if the number of neighbors is 2 or 3
nextnext[row][column] = 1 # value of target cell stays 1 in nextnext
elif neighbors > 3: # if the number of neigbors is higher than 3
nextnext[row][column] = 0 # value of target cell becomes 0 in nextnext
elif current [row][column] == 0: # in case a target cell has a value of 0:
if neighbors == 3: # if the number of neighbors is 3
nextnext[row][column] = 1 # value of target cell becomes 1 in nextnext
elif neighbors != 3: # if the number of neigbors isn't 3
nextnext[row][column] = 0 # value of target cell stays 0 in nextnext
return nextnext
for t in range(TIME): # determine amount of times the loop will
nextnext = create_empty_universum(seed)
for row in range(numrows): # for each
for column in range(numcolumns): # for each column
nextnext = compute_new_value(nextnext,row,column)
current = nextnext # make nextnext matrix the current matrix for the next step
life.append(current) # add current to life
import show
show.show(life, SIZE=10)