我尝试在PyCharm中运行我的代码,但得到的语法无效,抱怨它在第一个parantes之前缺少一个]。
为什么?我只有两个[[]]-括号,最后一个]在该行的末尾。
/最佳
import random
#Set up the grid;
class grid(object):
def __init__(self, mines, grid_size):
self.mines = mines
self.grid = grid[['0' for i in range(int(grid_size))] for i in range(int(grid_size))] #"null matrix"
mines = place_mines(grid, mines) #place where the mines should be randomly
for i,j in mines:
grid[i][j]="x" #i row, j colomn
grid=number(grid)
return (grid, mines)
输出为:
File "/Users/,/Deskop/minröj ej klas/minrlj klass.py", line 8
self.grid = grid[['0' for i in range(int(grid_size))] for i in range(int(grid_size))] #"null matrix"
^
SyntaxError: invalid syntax
Process finished with exit code 1
答案 0 :(得分:1)
您有一个grid
不应该存在的地方:
self.grid = [['0' for i in range(int(grid_size))] for i in range(int(grid_size))] #"null matrix"
列表理解语法(您使用的一种)如下:
foo = ["bar" for item in iterable]
因此,您只是没有使用应使用的语法。