0

私はPythonでライフゲームをプログラミングしていますが、奇妙な構文エラーに遭遇しました。それは私が定義している最初の関数にあります。nextnext=[]のnextnextが間違っていると言います。これを削除すると、forループの後に:もシンタックスエラーが発生します。別のファイルで関数を試しましたが、そこでは正常に機能したので、何が間違っているのか本当にわかりません。

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)
4

1 に答える 1

4

前の行に閉じ括弧がありません:

numcolumns = len(seed[0]    # calculate number of columns
# missing )   ----------^
nextnext = []               # create empty list next

Python 構文エラーが発生し、それを見つけることができない場合は、の行を見て、括弧、中括弧、および括弧のバランスが取れているかどうかを確認してください。

于 2013-03-19T14:57:35.643 に答える